-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcloneArray.js
More file actions
executable file
·108 lines (82 loc) · 1.71 KB
/
cloneArray.js
File metadata and controls
executable file
·108 lines (82 loc) · 1.71 KB
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// 34. Clone an Array
// 1. Using the Array.slice() Method
// We use the slice method to create a shallow copy of an array. This method creates a new array with a subset of the elements from the original array.
{
const a = [1, 2, 3];
const b = a.slice();
console.log(b);
}
// 2. Using the spread Operator
{
const a = [1, 2, 3];
const b = [...a];
console.log(b);
}
// 3. Using the Array.from() Method
{
const a = [1, 2, 3];
const b = Array.from(a);
console.log(b);
}
// 4. Using the Array.concat() Method
{
const a = [1, 2, 3];
const b = [].concat(a);
console.log(b);
}
// 5. Using a for loop
{
const a = [1, 2, 3];
const b = [];
for (let i = 0; i < a.length; i++) {
b.push(a[i]);
}
console.log(b);
}
// 6. Using the Array.map() Method
{
const a = [1, 2, 3];
const b = a.map((x) => x);
console.log(b);
}
// 7. Using the Array.from() method with a map function
{
const a = [1, 2, 3];
const b = Array.from(a, (x) => x);
console.log(b);
}
// 8. Using the Array.of() Method
{
const a = [1, 2, 3];
const b = Array.of(...a);
console.log(b);
}
// 9. Using the JSON.parse() and JSON.stringify() Methods
{
const a = [1, 2, 3];
const b = JSON.parse(JSON.stringify(a));
console.log(b);
}
// 10. Using the Object.assign() Method
{
const a = [1, 2, 3];
const b = Object.assign([], a);
console.log(b);
}
// 11. Using Array.reduce() Method
{
const a = [1, 2, 3, 4];
const b = a.reduce((acc, val) => {
acc.push(val);
return acc;
}, []);
console.log("Original Array:", a);
console.log("Cloned Array:", b);
}
// 12. Using the Array.flatMap() Method
{
const a = [2, 3, 4, 5];
const b = a.flatMap((elm) => [elm]);
console.log(b);
console.log(a === b);
}