How to copy an Array ? | JavaScript Interview Question

preview_player
Показать описание
Рекомендации по теме
Комментарии
Автор

I use 1 of 3 ways:




1: let b = [...a];
2 : let b = Object.values(a);
3 : let b = a.slice();

angshu
Автор

b = Object.assign([], a) will create empty array even if `a` is undefined,
but b = [...a] will throw an error.

UdayGhulaxe
Автор

your videos solve all my (JavaScript) problems :)

HiHi-qdoy
Автор

It was really a good session. One more way that I can think of is 'slice()'. I can do like this:-
let b = a.slice(0);
Slice() is actually for deletion, but passing '0' as arg, it will do the trick. 🙂

abhijeetsoni
Автор

let a = [1, 2, 3, 4]
let b = a.slice()

yogasound
Автор

Great video. You do a great job explaining every step. More videos like this please.


Edit: just found your other channel that has more videos like this :)

sogggy
Автор

Excellent initiative... thanks for covering such important topics now a days lot of questions come from oops in JavaScript

mdshoaibAlamgcetts
Автор

Hi, I wanna thank u for showing us all those ways ! keep it up !

mrzack
Автор

i can do with spread operator, with slice method
a=[1, 2, 3, 4, 5]
b=[...a]


b=a.slice(0, 5);

mohsanabbas
Автор

I would think the simplest method that's also backwards compatible (going waaay back) is: b = a.slice();

sergey_is_sergey
Автор

Hi Sir i Liked your tutorials, can you please make tutorials on Searching and sorting

princeroy
Автор

can we use slice as well?


let arr = [3, 4, 3];
let newarr= arr.slice(arr);

console.log(newarr)

Varunkumarmca
Автор

The lack of spaces in your for loop upsets my ocd. Other than that, great lesson.

RedEyedJedi
Автор

if interviewer ask don't use any method even under loop ?

irkfaisal
Автор

Hi sir i watched your videos your explanation is very good .
can u explain different ways of create an object in javaScript ?

sureshkumar-xdot
Автор

sir I have one question -suppose I have a set of boxes suppose 5*5 (25 boxes) and when we have clicked on any box we need to display number on which box we have clicked on ?how to solve this?

jainshilpi
Автор

One note, for objects, spread operator or Object.assign copy references not clone the object.

akalrove
Автор

I was doing this.a = Object.assign([ ], this.b ); today, but noticed b was still changing on a.

So I tried with, this.a = which worked well

sasta_guitarist
Автор

Why not just do 

let b = [1, 2, 3, 4];

?

I'll show myself out ...

Palalune
Автор

how to copy 2 arrays in to 1 array i have an exam thanks for help

AliNjaff