10 examples of 'js array remove element by index' in JavaScript

Every line of 'js array remove element by index' 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
30public removeAt(index: number): this {
31 const argumentName = this._getNextArgumentName();
32
33 this._scriptLines.push("this." + this._pathToArray + ".splice(args." + argumentName + ", 1);");
34 this._parameters[argumentName] = index;
35 return this;
36}
48export function remove(array, index) {
49 var len = array.length - 1;
50
51 var out = new Array(len);
52
53 var i = 0;
54 while (i < index) {
55 out[i] = array[i];
56 ++i;
57 }
58
59 while (i < len) {
60 out[i] = array[i + 1];
61 ++i;
62 }
63
64 return out;
65}
11static remove(array, index, callback) {
12 array.splice(index, 1);
13 if (callback) {
14 callback();
15 }
16}
86public static remove(array: T[], element: any | ((element: T) => boolean), options: { firstOnly: boolean }): T[] {
87 // define a function to resolve the element's index in the original array
88 const indexOfElementFn = ((): () => number => {
89 if (typeof element === 'function') {
90 return (): number => array.findIndex(element);
91 }
92 else {
93 return (): number => array.indexOf(element);
94 }
95 })();
96
97 const removedElements = [];
98 for (let i = indexOfElementFn(); i !== -1; i = indexOfElementFn()) {
99 removedElements.push(...array.splice(i, 1)); // changes the original array
100 if (options.firstOnly) {
101 break;
102 }
103 }
104 return removedElements;
105}
98export function remove__index(array, index) {
99 return array.splice(index, 1)
100}
13function arrayRemove (array, el) {
14 let index = array.indexOf(el)
15 if (index > -1) {
16 array.splice(index, 1)
17 }
18 return index
19}
279removeElementAtIndex(index: number): T | undefined {
280 if (index < 0 || index >= this.nElements || this.firstNode === null || this.lastNode === null) {
281 return undefined;
282 }
283 let element: T | undefined;
284 if (this.nElements === 1) {
285 //First node in the list.
286 element = this.firstNode.element;
287 this.firstNode = null;
288 this.lastNode = null;
289 } else {
290 const previous = this.nodeAtIndex(index - 1);
291 if (previous === null) {
292 element = this.firstNode.element;
293 this.firstNode = this.firstNode.next;
294 } else if (previous.next === this.lastNode) {
295 element = this.lastNode.element;
296 this.lastNode = previous;
297 }
298 if (previous !== null && previous.next !== null) {
299 element = previous.next.element;
300 previous.next = previous.next.next;
301 }
302 }
303 this.nElements--;
304 return element;
305}
11at (index, value) {
12 if (value !== undefined) {
13 if (this._array[index] !== undefined) {
14 this._removeValue(this._array[index])
15 }
16
17 this._addValue(this._array[index] = value)
18 }
19
20 return this._array[index]
21}
54function remove_element(array, elem) {
55 var index = array.indexOf(elem);
56 if (index > -1) {
57 array.splice(index, 1);
58 }
59}
171function remove(array, el) {
172 var i;
173 for (i = array.length - 1; i > -1; i-=1) {
174 if (array[i] === el) {
175 array.splice(i, 1);
176 }
177 }
178}

Related snippets