-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharray-split.js
More file actions
33 lines (28 loc) · 1.03 KB
/
Copy patharray-split.js
File metadata and controls
33 lines (28 loc) · 1.03 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
const nums = [1, 2, 3, 4, 5, 6, 7, 8];
/*
=====slice()======
starts from 2-index and ends before 5-index(4-index)
keeps the array full without loosing any element
*/
// const arrSlice = nums.slice(2, 5);
// console.log(arrSlice); //output-> 3, 4 5
// console.log(nums); //output-> 1, 2, 3, 4, 5, 6, 7, 8
/*
=====splice()======
takes 3 elements from 2-index
*/
// const arrSplice = nums.splice(2, 3);
// console.log(arrSplice); //output-> 3 4 5
// console.log(nums); //output-> 1 2 6 7 8
//takes 3 elements from 2-index then includes 10, 20, 30 to the removed indexes
// const arrSpliceAddElement = nums.splice(2, 3, 10, 20, 30);
// console.log(arrSpliceAddElement); //output-> 3 4 5
// console.log(nums); //output-> 1 2 10 20 30 6 7 8
/*
=====join()======
takes 3 elements from 2-index
*/
//takes 3 elements from 3-index then includes 10, 20,30 to the removed indexes
const removedAndInclude = nums.splice(3, 3, 10, 20, 30); //removes 4 5 6
const joinElements = nums.join(" > "); //output-> 1 > 2 > 10 > 20 > 30 > 6 > 7 > 8
console.log(joinElements);