-
Notifications
You must be signed in to change notification settings - Fork 61
/
Splice-Polyfill.js
125 lines (94 loc) · 3.21 KB
/
Splice-Polyfill.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
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
/* 💡"JavaScript-with-JC"
👉Array.prototype.splice and Its Polyfill
Array.prototype.splice modifies an original array and returns deleted values array.
💡splice method takes (start, howManyDelete, newAdd1, newAdd2, newAddN), If no argument is passed then original array remains as it is and it returns an empty array [].
positive index => 0 1 2
💡for an array = [10,20,30]
negative index => -3 -2 -1
💡Example -
👉 CASE 1 :- (start index is positive)
const numbers = [10, 11, 12, 13, 14, 15];
const deletedNumbers = numbers.splice(1, 3, 77, 88, 99);
here, startIndex = 1, deleteCount = 3, addNewValue = [77,88,99]
console.log(numbers); [ 10, 77, 88, 99, 14, 15 ]
console.log(deletedNumbers); [ 11, 12, 13 ]
👉 CASE 2 :- (start index is negative)
const numbers = [10, 11, 12, 13, 14, 15];
const deletedNumbers = numbers.splice(-4, 3, 77, 88, 99);
here, startIndex = -4, deleteCount = 3, addNewValue = [77,88,99]
console.log(numbers); [ 10, 11, 77, 88, 99, 15 ]
console.log(deletedNumbers); [ 12, 13, 14 ]
💡Note - splice mutates the original array, and returns deleted values array.
💡Use Case -
👉 deleting a data from list of data
Let's take example of deleting a todo from todolist 👇
const todos = [
{ id: 1, todo: "Morning Walk" },
{ id: 2, todo: "Go to Office" },
{ id: 3, todo: "Go to Gym" },
{ id: 4, todo: "Watch Netflix" },
];
const deleteId = 2;
const findIndex = todos.findIndex(({ id }) => id === deleteId);
todos.splice(findIndex, 1);
console.log(todos);
output 👇
[
{ id: 1, todo: 'Morning Walk' },
{ id: 3, todo: 'Go to Gym' },
{ id: 4, todo: 'Watch Netflix' }
]
👉 One Level Up :- We can create our own custom splice( Polyfill of splice ), Check out the code below.👇
*/
const numbers = [10, 11, 12, 13, 14, 15];
const deletedNumbers = numbers.splice(2, 3, 77, 88);
console.log("numbers", numbers);
console.log("deletedNumbers", deletedNumbers);
Array.prototype.customSplice = function (start, deleteCount) {
let array = this;
if (start !== undefined && deleteCount === undefined) {
deleteCount = array.length;
}
start = Number(start);
if (start < 0) {
start = array.length + start;
}
if (isNaN(start)) {
start = 0;
}
if (isNaN(deleteCount) || deleteCount < 0) {
deleteCount = 0;
}
let end = start + Number(deleteCount);
let valuesBeforeStart = [];
let SplicedArray = [];
let valuesAfterSplice = [];
for (let i = 0; i < array.length; i++) {
if (i < start) {
valuesBeforeStart.push(array[i]);
}
if (i >= start && i < end) {
SplicedArray.push(array[i]);
}
if (i >= end && i < array.length) {
valuesAfterSplice.push(array[i]);
}
}
for (let i = 2; i < arguments.length; i++) {
valuesBeforeStart.push(arguments[i]);
}
let result = valuesBeforeStart.concat(valuesAfterSplice);
let len = Math.max(array.length, result.length);
for (i = 0; i < len; i++) {
if (result.length > i) {
array[i] = result[i];
} else {
array.pop();
}
}
return SplicedArray;
};
const customNumbers = [10, 11, 12, 13, 14, 15];
const deletedCustomNums = customNumbers.customSplice(2, 3, 77, 88);
console.log(customNumbers); // [10, 11, 77, 88, 15]
console.log(deletedCustomNums); // [12, 13, 14]