-
Notifications
You must be signed in to change notification settings - Fork 0
/
Clone_An_Array.js
46 lines (34 loc) · 873 Bytes
/
Clone_An_Array.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
/*
Clone an Array
4 ways to create a clone
1. Spread Operator
2. Filter
3. Map
4. Array.from
*/
//Using the spread operator we can make a clone of an array
var arr = [10, 20, 30, 40, 50, 60];
var arr1 = [...arr];
arr1;
(6) [10, 20, 30, 40, 50, 60]
arr1 === arr
false
//Without using the spread operator we can also make a clone of an array (using filter method)
var g = arr.filter(w=>true); // we are creating a sub array
g;
(6) [10, 20, 30, 40, 50, 60]
g === arr1;
false
// we can also make a clone of an array (using map method)
// map creates the array of same length
var g = arr.map(w=>w); // we are creating a sub array
g;
(6) [10, 20, 30, 40, 50, 60]
g === arr1;
false
var arr4 = Array.from(arr); // creating a clone
undefined
arr4;
(6) [10, 20, 30, 40, 50, 60]
arr4 === arr; // reference to different location
false