7 examples of 'mdn array splice' in JavaScript

Every line of 'mdn array splice' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your JavaScript code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
53function splice (arr, start, remove, ...rest) {
54 let newArr = []
55 let indexOfNewArr = 0
56 for (let i = 0; i < arr.length; i++) {
57 // Jumping the data that want to be removed
58 if (i >= start && i < start + remove) {
59 continue
60 }
61 // Insert to the array
62 newArr[indexOfNewArr] = arr[i]
63 indexOfNewArr++
64 // If there are any new data that want to be added, insert from index start
65 if (indexOfNewArr === start) {
66 for (let j = 0; j < rest.length; j++) {
67 newArr[indexOfNewArr] = rest[j]
68 indexOfNewArr++
69 }
70 }
71 }
72 return newArr
73}
217function spliceArray(array: AST.Node[], index: number, result: AST.Node | AST.Node[] | null) {
218 if (result === null) {
219 array.splice(index, 1);
220 return 0;
221 } else if (Array.isArray(result)) {
222 array.splice(index, 1, ...result);
223 return result.length;
224 } else {
225 array.splice(index, 1, result);
226 return 1;
227 }
228}
90function splice(list, i) {
91 if (i === 0) {
92 list.shift();
93 return;
94 }
95
96 let k = i + 1;
97
98 while (k < list.length)
99 list[i++] = list[k++];
100
101 list.pop();
102}
27static remove(array = [], index = 0) {
28 return index >= 0 ? array.splice(index, 1) : [];
29}
122splice(...args) {
123 return this._mutation('splice', args);
124}
199static splice(l:List, from:int, length:int):List {
200 return l.splice(from, length);
201}
64export function arrayShift(arr: T[]): T {
65 return arr.removeAt(0);
66}

Related snippets