How to easily clone an array in javascript #shorts

preview_player
Показать описание
sharing some useful javascript tips for cloning an array
------------

Рекомендации по теме
Комментарии
Автор

I am glad you did not mention the JSON stringify/decode method. Thanks for raising a better generation of devs.

neirenoir
Автор

Having my daily dose of javascript tips and tricks. Great work! 💯🙌

donaldjr.labajo
Автор

Keep in mind that this only make shallow copy not a deep copy, so if you have nested objects or arrays you will have references issues

urkoGaming
Автор

Your videos are always so helpful, thank you man

QuadDrums
Автор

I've been using the .slice() approach almost exclusively. Gotta use that spread operator more...

re.liable
Автор

i think [...array] is the most elegant. Also works great in functions like function test(...args) and you can have dynamic arguments for your function.

AndrewTSq
Автор

drop these methods, this year the *structuredClone* proposal was approved and implemented, it does deep clone and is a native method.

hadawardgz
Автор

Nice... Also one thing you might run into is. When you clone nested objects... The spread or slice will keep the reference l. So you will need to do a shallow clone or use a utility library CloneDeep

sukhsingh
Автор

Thanks but, what font are you using? Is this font only exclusive to macbooks?

bryllem.
Автор

what about cloning objects? I there any short about that? maybe you can explain the bad practice of using json stringify parse and use structuredClone

lolobirolo
Автор

you can test it by doing:
let arr1 = [1, 2, 3, 4]
let arr2 = arr1
let arr3 = [...arr1]
arr1[0] = 0
console.log(arr2) // [0, 2, 3, 4]
console.log(arr3) // [1, 2, 3, 4]

it's the same with objects

reaper_
Автор

is there an extension pack that you use

aboi
Автор

Very useful for mutable methods in your use cases =)

victorj
Автор

Brother. How to each word first letter capitalize..
Like,
Hello Everyone How Are You?

tharindumayuranga