-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsliceArray.js
More file actions
executable file
·45 lines (31 loc) · 801 Bytes
/
sliceArray.js
File metadata and controls
executable file
·45 lines (31 loc) · 801 Bytes
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
// 22. Splice an Array Without Mutating the Original Array
//1. Using slice() and concat() Methods - Most Used
{
const a1 = [1, 2, 3, 4, 5];
const a2 = a1.slice(0, 1);
console.log(a2);
}
//2. Using filter() Method for Conditional Removal
{
const a1 = [1, 2, 3, 4, 5];
const a2 = a1.filter((_, index) => index !== 1 && index !== 2);
console.log(a2);
console.log(a1);
}
//3. Using the Spread Operator and slice() Method
{
const a1 = [1, 2, 3, 4, 5];
const a2 = [...a1.slice(0, 1), ...a1.slice(3)];
console.log(a2);
console.log(a1);
}
//4. Using map() Method with Conditionals
{
const a1 = [1, 2, 3, 4, 5];
const a2 = a1
.map((el, index) => (index === 1 || index === 2 ? null : el))
.filter((el) => el !== null);
console.log(a2);
console.log(a1);
}
//