· 7 years ago · Apr 27, 2018, 07:36 AM
1var array = [2, 5, 9];
2var index = array.indexOf(5);
3if (index > -1) {
4 array.splice(index, 1);
5}
6// array = [2, 9]
7
8Array.prototype.indexOf || (Array.prototype.indexOf = function(d, e) {
9 var a;
10 if (null == this) throw new TypeError('"this" is null or not defined');
11 var c = Object(this),
12 b = c.length >>> 0;
13 if (0 === b) return -1;
14 a = +e || 0;
15 Infinity === Math.abs(a) && (a = 0);
16 if (a >= b) return -1;
17 for (a = Math.max(0 <= a ? a : b - Math.abs(a), 0); a < b;) {
18 if (a in c && c[a] === d) return a;
19 a++
20 }
21 return -1
22});
23
24array.splice(i, 1);
25
26for(var i = array.length - 1; i >= 0; i--) {
27 if(array[i] === number) {
28 array.splice(i, 1);
29 }
30}
31
32delete array[i];
33
34var value = 3
35
36var arr = [1, 2, 3, 4, 5, 3]
37
38arr = arr.filter(function(item) {
39 return item !== value
40})
41
42console.log(arr)
43// [ 1, 2, 4, 5 ]
44
45let value = 3
46
47let arr = [1, 2, 3, 4, 5, 3]
48
49arr = arr.filter(item => item !== value)
50
51console.log(arr)
52// [ 1, 2, 4, 5 ]
53
54let forDeletion = [2, 3, 5]
55
56let arr = [1, 2, 3, 4, 5, 3]
57
58arr = arr.filter(item => !forDeletion.includes(item))
59// !!! Read below about array.includes(...) support !!!
60
61console.log(arr)
62// [ 1, 4 ]
63
64// array-lib.js
65
66export function remove(...forDeletion) {
67 return this.filter(item => !forDeletion.includes(item))
68}
69
70// main.js
71
72import { remove } from './array-lib.js'
73
74let arr = [1, 2, 3, 4, 5, 3]
75
76// :: This-Binding Syntax Proposal
77// using "remove" function as "virtual method"
78// without extending Array.prototype
79arr = arr::remove(2, 3, 5)
80
81console.log(arr)
82// [ 1, 4 ]
83
84delete array[ index ];
85
86array.splice( index, 1 );
87
88var value = array.splice( index, 1 )[0];
89
90function remove(arr, item) {
91 for(var i = arr.length; i--;) {
92 if(arr[i] === item) {
93 arr.splice(i, 1);
94 }
95 }
96 }
97
98Array.prototype.remByVal = function(val) {
99 for (var i = 0; i < this.length; i++) {
100 if (this[i] === val) {
101 this.splice(i, 1);
102 i--;
103 }
104 }
105 return this;
106}
107//Call like
108[1, 2, 3, 4].remByVal(3);
109
110function move(arr, val) {
111 var j = 0;
112 for (var i = 0, l = arr.length; i < l; i++) {
113 if (arr[i] !== val) {
114 arr[j++] = arr[i];
115 }
116 }
117 arr.length = j;
118}
119
120function indexof(arr, val) {
121 var i;
122 while ((i = arr.indexOf(val)) != -1) {
123 arr.splice(i, 1);
124 }
125}
126
127function splice(arr, val) {
128 for (var i = arr.length; i--;) {
129 if (arr[i] === val) {
130 arr.splice(i, 1);
131 }
132 }
133}
134
135Remove all occurrences:
136 move 0.0048 ms
137 indexof 0.0463 ms
138 splice 0.0359 ms
139
140Remove first occurrence:
141 move_one 0.0041 ms
142 indexof_one 0.0021 ms
143
144var removed = helper.removeOne(arr, row => row.id === 5 );
145
146var removed = helper.remove(arr, row => row.name.startsWith('BMW'));
147
148var helper = {
149
150 // Remove and return the first occurrence
151
152 removeOne: function(array, predicate) {
153 for (var i = 0; i < array.length; i++) {
154 if (predicate(array[i])) {
155 return array.splice(i, 1);
156 }
157 }
158 },
159
160 // Remove and return all occurrences
161
162 remove: function(array, predicate) {
163 var removed = [];
164
165 for (var i = 0; i < array.length;) {
166
167 if (predicate(array[i])) {
168 removed.push(array.splice(i, 1));
169 continue;
170 }
171
172 i++;
173 }
174
175 return removed;
176 }
177};
178
179// Array Remove - By John Resig (MIT Licensed)
180Array.prototype.remove = function(from, to) {
181 var rest = this.slice((to || from) + 1 || this.length);
182 this.length = from < 0 ? this.length + from : from;
183 return this.push.apply(this, rest);
184};
185
186// Array Remove - By John Resig (MIT Licensed)
187Array.remove = function(array, from, to) {
188 var rest = array.slice((to || from) + 1 || array.length);
189 array.length = from < 0 ? array.length + from : from;
190 return array.push.apply(array, rest);
191};
192
193Array.prototype.remove = function(from, to){
194 this.splice(from, (to=[0,from||1,++to-from][arguments.length])<0?this.length+to:to);
195 return this.length;
196};
197
198myArray.remove(8);
199
200_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]
201
202function remove(arrOriginal, elementToRemove){
203 return arrOriginal.filter(function(el){return el !== elementToRemove});
204}
205console.log( remove([1, 2, 1, 0, 3, 1, 4], 1) );
206
207var array=['1','2','3','4','5','6']
208var index = array.filter((value)=>value!='3');
209
210["1", "2", "4", "5", "6"]
211
212var my_array = [1,2,3,4,5,6];
213delete my_array[4];
214console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));
215
216remove_item = function (arr, value) {
217 var b = '';
218 for (b in arr) {
219 if (arr[b] === value) {
220 arr.splice(b, 1);
221 break;
222 }
223 }
224 return arr;
225}
226
227remove_item(array,value);
228
229var array1 = ['a', 'b', 'c', 'd']
230_.pull(array1, 'c')
231console.log(array1) // ['a', 'b', 'd']
232
233var array2 = ['e', 'f', 'g', 'h']
234_.pullAt(array2, 0)
235console.log(array2) // ['f', 'g', 'h']
236
237var array3 = ['i', 'j', 'k', 'l']
238var newArray = _.without(array3, 'i') // ['j', 'k', 'l']
239console.log(array3) // ['i', 'j', 'k', 'l']
240
241var num = [1, 2, 3, 4, 5];
242
243num.splice(num.indexOf(4), 1); //num will be [1, 2, 3, 5];
244
245Array.prototype.remove = Array.prototype.remove || function(x) {
246 const i = this.indexOf(x);
247 if(i===-1) return;
248 this.splice(i, 1); //num.remove(5) === [1, 2, 3];
249}
250
251var num = [5, 6, 5, 4, 5, 1, 5];
252
253const _removeValue = (arr, x) => arr.filter(n => n!==x);
254//_removeValue([1, 2, 3, 4, 5, 5, 6, 5], 5) //return [1, 2, 3, 4, 6]
255
256function removeFromArray(array, item, index) {
257 while((index = array.indexOf(item)) > -1) {
258 array.splice(index, 1);
259 }
260}
261
262//Set-up some dummy data
263var dummyObj = {name:"meow"};
264var dummyArray = [dummyObj, "item1", "item1", "item2"];
265
266//Remove the dummy data
267removeFromArray(dummyArray, dummyObj);
268removeFromArray(dummyArray, "item2");
269
270const removeByIndex = (list, index) =>
271 [
272 ...list.slice(0, index),
273 ...list.slice(index + 1)
274 ];
275
276removeByIndex([33,22,11,44],1) //=> [33,11,44]
277
278this.array = this.array.filter(function(element, i) {
279 return element.id !== idToRemove;
280});
281
282Array.prototype.destroy = function(obj){
283 // Return null if no objects were found and removed
284 var destroyed = null;
285
286 for(var i = 0; i < this.length; i++){
287
288 // Use while-loop to find adjacent equal objects
289 while(this[i] === obj){
290
291 // Remove this[i] and store it within destroyed
292 destroyed = this.splice(i, 1)[0];
293 }
294 }
295
296 return destroyed;
297}
298
299var x = [1, 2, 3, 3, true, false, undefined, false];
300
301x.destroy(3); // => 3
302x.destroy(false); // => false
303x; // => [1, 2, true, undefined]
304
305x.destroy(true); // => true
306x.destroy(undefined); // => undefined
307x; // => [1, 2]
308
309x.destroy(3); // => null
310x; // => [1, 2]
311
312const items = [1, 2, 3, 4];
313const index = 2;
314
315items.filter((x, i) => i !== index);
316
317[1, 2, 4]
318
319var myArray = [1,2,3,4,5,6];
320
321myArray = myArray.filter(value => value !== 5);
322
323[1,2,3,4,6]; // 5 has been removed from this array
324
325function removeAll(array, key){
326 var index = array.indexOf(key);
327
328 if(index === -1) return;
329
330 array.splice(index, 1);
331 removeAll(array,key);
332}
333
334Array.prototype.removeAll = function(key){
335 var index = this.indexOf(key);
336
337 if(index === -1) return;
338
339 this.splice(index, 1);
340 this.removeAll(key);
341}
342
343var myElement = "chocolate";
344var myArray = ['chocolate', 'poptart', 'poptart', 'poptart', 'chocolate', 'poptart', 'poptart', 'chocolate'];
345
346/* Important code */
347for (var i = myArray.length - 1; i >= 0; i--) {
348 if (myArray[i] == myElement) myArray.splice(i, 1);
349}
350
351/**
352* removeByIndex
353* @param {Array} array
354* @param {Number} index
355*/
356function removeByIndex(array, index){
357 return array.filter(function(elem, _index){
358 return index != _index;
359 });
360}
361l = [1,3,4,5,6,7];
362console.log(removeByIndex(l, 1));
363
364$> [ 1, 4, 5, 6, 7 ]
365
366/**
367* removeByValue
368* @param {Array} array
369* @param {Number} value
370*/
371function removeByValue(array, value){
372 return array.filter(function(elem, _index){
373 return value != elem;
374 });
375}
376l = [1,3,4,5,6,7];
377console.log(removeByValue(l, 5));
378
379$> [ 1, 3, 4, 6, 7]
380
381myRecursiveFunction(myArr.slice(0,i).concat(a.slice(i+1)))
382
383myRecursiveFunction((myArr.push(c),myArr))
384
385const a = {'field': 2} // Non-primitive object
386const b = {'field': 2} // Non-primitive object with same value
387const c = a // Non-primitive object that reference the same object as "a"
388
389assert(a !== b) // Don't reference the same item, but have same value
390assert(a === c) // Do reference the same item, and have same value (naturally)
391
392//Note: there are many alternative implementations for valuesAreEqual
393function valuesAreEqual (x, y) {
394 return JSON.stringify(x) === JSON.stringify(y)
395}
396
397
398//filter will delete false values
399//Thus, we want to return "false" if the item
400// we want to delete is equal to the item in the array
401function removeFromArray(arr, toDelete){
402 return arr.filter(target => {return !valuesAreEqual(toDelete, target)})
403}
404
405const exampleArray = [a, b, b, c, a, {'field': 2}, {'field': 90}];
406const resultArray = removeFromArray(exampleArray, a);
407
408//resultArray = [{'field':90}]
409
410function arrayWithout(arr, values) {
411 var isArray = function(canBeArray) {
412 if (Array.isArray) {
413 return Array.isArray(canBeArray);
414 }
415 return Object.prototype.toString.call(canBeArray) === '[object Array]';
416 };
417
418 var excludedValues = (isArray(values)) ? values : [].slice.call(arguments, 1);
419 var arrCopy = arr.slice(0);
420
421 for (var i = arrCopy.length - 1; i >= 0; i--) {
422 if (excludedValues.indexOf(arrCopy[i]) > -1) {
423 arrCopy.splice(i, 1);
424 }
425 }
426
427 return arrCopy;
428}
429
430const arrayWithoutFastest = (() => {
431 const isArray = canBeArray => ('isArray' in Array)
432 ? Array.isArray(canBeArray)
433 : Object.prototype.toString.call(canBeArray) === '[object Array]';
434
435 let mapIncludes = (map, key) => map.has(key);
436 let objectIncludes = (obj, key) => key in obj;
437 let includes;
438
439 function arrayWithoutFastest(arr, ...thisArgs) {
440 let withoutValues = isArray(thisArgs[0]) ? thisArgs[0] : thisArgs;
441
442 if (typeof Map !== 'undefined') {
443 withoutValues = withoutValues.reduce((map, value) => map.set(value, value), new Map());
444 includes = mapIncludes;
445 } else {
446 withoutValues = withoutValues.reduce((map, value) => { map[value] = value; return map; } , {});
447 includes = objectIncludes;
448 }
449
450 const arrCopy = [];
451 const length = arr.length;
452
453 for (let i = 0; i < length; i++) {
454 // If value is not in exclude list
455 if (!includes(withoutValues, arr[i])) {
456 arrCopy.push(arr[i]);
457 }
458 }
459
460 return arrCopy;
461 }
462
463 return arrayWithoutFastest;
464})();
465
466const arr = [1,2,3,4,5,"name", false];
467
468arrayWithoutFastest(arr, 1); // will return array [2,3,4,5,"name", false]
469arrayWithoutFastest(arr, 'name'); // will return [2,3,4,5, false]
470arrayWithoutFastest(arr, false); // will return [2,3,4,5]
471arrayWithoutFastest(arr,[1,2]); // will return [3,4,5,"name", false];
472arrayWithoutFastest(arr, {bar: "foo"}); // will return the same array (new copy)
473
474var my_array = new Array();
475
476my_array.push("element1");
477
478var indexOf = function(needle)
479{
480 if(typeof Array.prototype.indexOf === 'function') // newer browsers
481 {
482 indexOf = Array.prototype.indexOf;
483 }
484 else // older browsers
485 {
486 indexOf = function(needle)
487 {
488 var index = -1;
489
490 for(var i = 0; i < this.length; i++)
491 {
492 if(this[i] === needle)
493 {
494 index = i;
495 break;
496 }
497 }
498 return index;
499 };
500 }
501
502 return indexOf.call(this, needle);
503};
504
505var index = indexOf.call(my_array, "element1");
506
507my_array.splice(index, 1);