javascript arrays

array.remove(number);

const array = [2, 5, 9]; console.log(array); const index = array.indexOf(5); if (index > -1) { array.splice(index, 1); } // array = [2, 9] console.log(array);

array.splice(i, 1);

for(var i = array.length - 1; i >= 0; i--) { if(array[i] === number) { array.splice(i, 1); } }

delete array[i];

var value = 3 var arr = [1, 2, 3, 4, 5, 3] arr = arr.filter(function(item) { return item !== value }) console.log(arr) // [ 1, 2, 4, 5 ]

let value = 3 let arr = [1, 2, 3, 4, 5, 3] arr = arr.filter(item => item !== value) console.log(arr) // [ 1, 2, 4, 5 ]

let forDeletion = [2, 3, 5] let arr = [1, 2, 3, 4, 5, 3] arr = arr.filter(item => !forDeletion.includes(item)) // !!! Read below about array.includes(...) support !!! console.log(arr) // [ 1, 4 ]

// array-lib.js export function remove(...forDeletion) { return this.filter(item => !forDeletion.includes(item)) } // main.js import { remove } from './array-lib.js' let arr = [1, 2, 3, 4, 5, 3] // :: This-Binding Syntax Proposal // using "remove" function as "virtual method" // without extending Array.prototype arr = arr::remove(2, 3, 5) console.log(arr) // [ 1, 4 ]

delete array[index];

array.splice(index, 1);

var value = array.splice(index, 1)[0];

function remove(arr, item) { for (var i = arr.length; i--;) { if (arr[i] === item) { arr.splice(i, 1); } } }

Array.prototype.remByVal = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] === val) { this.splice(i, 1); i--; } } return this; } //Call like [1, 2, 3, 4].remByVal(3);

Array.prototype.remByVal = function(val) { for (var i = 0; i < this.length; i++) { if (this[i] === val) { this.splice(i, 1); i--; } } return this; } var rooms = ['hello', 'something'] rooms = rooms.remByVal('hello') console.log(rooms)

function move(arr, val) { var j = 0; for (var i = 0, l = arr.length; i < l; i++) { if (arr[i] !== val) { arr[j++] = arr[i]; } } arr.length = j; }

function indexof(arr, val) { var i; while ((i = arr.indexOf(val)) != -1) { arr.splice(i, 1); } }

function splice(arr, val) { for (var i = arr.length; i--;) { if (arr[i] === val) { arr.splice(i, 1); } } }

Remove all occurrences: move 0.0048 ms indexof 0.0463 ms splice 0.0359 ms Remove first occurrence: move_one 0.0041 ms indexof_one 0.0021 ms

var removed = helper.removeOne(arr, row => row.id === 5 ); var removed = helper.remove(arr, row => row.name.startsWith('BMW'));

var helper = { // Remove and return the first occurrence removeOne: function(array, predicate) { for (var i = 0; i < array.length; i++) { if (predicate(array[i])) { return array.splice(i, 1); } } }, // Remove and return all occurrences remove: function(array, predicate) { var removed = []; for (var i = 0; i < array.length;) { if (predicate(array[i])) { removed.push(array.splice(i, 1)); continue; } i++; } return removed; } };

function remove(arrOriginal, elementToRemove){ return arrOriginal.filter(function(el){return el !== elementToRemove}); } console.log(remove([1, 2, 1, 0, 3, 1, 4], 1));

// Array Remove - By John Resig (MIT Licensed) Array.prototype.remove = function(from, to) { var rest = this.slice((to || from) + 1 || this.length); this.length = from < 0 ? this.length + from : from; return this.push.apply(this, rest); };

// Array Remove - By John Resig (MIT Licensed) Array.remove = function(array, from, to) { var rest = array.slice((to || from) + 1 || array.length); array.length = from < 0 ? array.length + from : from; return array.push.apply(array, rest); };

Array.prototype.remove = function(from, to){ this.splice(from, (to=[0,from||1,++to-from][arguments.length])<0?this.length+to:to); return this.length; };

myArray.remove(8);

_.without([1, 2, 1, 0, 3, 1, 4], 0, 1); // => [2, 3, 4]

var array=['1','2','3','4','5','6'] var newArray = array.filter((value)=>value!='3'); console.log(newArray);

["1", "2", "4", "5", "6"]

var my_array = [1, 2, 3, 4, 5, 6]; delete my_array[4]; console.log(my_array.filter(function(a){return typeof a !== 'undefined';}));

remove_item = function (arr, value) { var b = ''; for (b in arr) { if (arr[b] === value) { arr.splice(b, 1); break; } } return arr; }

remove_item(array,value);

var array1 = ['a', 'b', 'c', 'd'] _.pull(array1, 'c') console.log(array1) // ['a', 'b', 'd'] var array2 = ['e', 'f', 'g', 'h'] _.pullAt(array2, 0) console.log(array2) // ['f', 'g', 'h'] var array3 = ['i', 'j', 'k', 'l'] var newArray = _.without(array3, 'i') // ['j', 'k', 'l'] console.log(array3) // ['i', 'j', 'k', 'l']

var num = [1, 2, 3, 4, 5];

num.splice(num.indexOf(4), 1); // num will be [1, 2, 3, 5];

Array.prototype.remove = Array.prototype.remove || function(x) { const i = this.indexOf(x); if(i===-1) return; this.splice(i, 1); // num.remove(5) === [1, 2, 3]; }

var num = [5, 6, 5, 4, 5, 1, 5];

const _removeValue = (arr, x) => arr.filter(n => n!==x); //_removeValue([1, 2, 3, 4, 5, 5, 6, 5], 5) // Return [1, 2, 3, 4, 6]

const removeByIndex = (list, index) => [ ...list.slice(0, index), ...list.slice(index + 1) ]; output = removeByIndex([33,22,11,44],1) //=> [33,11,44] console.log(output)

const items = ['a', 'b', 'c', 'd', 'e', 'f'] const i = 3 const filteredItems = items.slice(0, i).concat(items.slice(i+1, items.length)) console.log(filteredItems)

const items = ['a', 'b', 'c', 'd', 'e', 'f'] const valueToRemove = 'c' const filteredItems = items.filter(item => item !== valueToRemove) console.log(filteredItems)

const items = ['a', 'b', 'c', 'd', 'e', 'f'] const valueToRemove = 'c' const filteredItems = items.filter(function(item) { return item !== valueToRemove }) console.log(filteredItems)

const items = ['a', 'b', 'c', 'd', 'e', 'f'] const removeItem = (items, i) => items.slice(0, i-1).concat(items.slice(i, items.length)) let filteredItems = removeItem(items, 3) filteredItems = removeItem(filteredItems, 5) //["a", "b", "c", "d"] console.log(filteredItems)

const items = ['a', 'b', 'c', 'd', 'e', 'f'] const valuesToRemove = ['c', 'd'] const filteredItems = items.filter(item => !valuesToRemove.includes(item)) // ["a", "b", "e", "f"] console.log(filteredItems)

theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1);

if (theArray.indexOf("stringToRemoveFromArray") >= 0){ theArray.splice(theArray.indexOf("stringToRemoveFromArray"), 1); }

var array=['1','2','3','4','5','6'] var newArray = array.filter((value)=>value!='3');

function removeFromArray(array, item, index) { while((index = array.indexOf(item)) > -1) { array.splice(index, 1); } }

//Set-up some dummy data var dummyObj = {name:"meow"}; var dummyArray = [dummyObj, "item1", "item1", "item2"]; //Remove the dummy data removeFromArray(dummyArray, dummyObj); removeFromArray(dummyArray, "item2");

this.array = this.array.filter(function(element, i) { return element.id !== idToRemove; });

Array.prototype.destroy = function(obj){ // Return null if no objects were found and removed var destroyed = null; for(var i = 0; i < this.length; i++){ // Use while-loop to find adjacent equal objects while(this[i] === obj){ // Remove this[i] and store it within destroyed destroyed = this.splice(i, 1)[0]; } } return destroyed; }

var x = [1, 2, 3, 3, true, false, undefined, false]; x.destroy(3); // => 3 x.destroy(false); // => false x; // => [1, 2, true, undefined] x.destroy(true); // => true x.destroy(undefined); // => undefined x; // => [1, 2] x.destroy(3); // => null x; // => [1, 2]

let arr = [1,2,3,4];

arr.splice(2, 1); //=> arr became [1,2,4]

arr = arr.filter(e => e !== 3); //=> arr became [1,2,4]

var myArray = [1, 2, 3, 4, 5, 6];

myArray = myArray.filter(value => value !== 5);

[1, 2, 3, 4, 6]; // 5 has been removed from this array

const items = [1, 2, 3, 4]; const index = 2;

items.filter((x, i) => i !== index);

[1, 2, 4]

var numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var newNumberArray = numberArray.filter(m => { return m !== 5; }); console.log("new Array, 5 removed", newNumberArray);

var numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var newNumberArray = numberArray.filter(m => { return (m !== 1) && (m !== 7) && (m !== 8); }); console.log("new Array, 1,7 and 8 removed", newNumberArray);

var numberArray = [1, 2, 3, 4, 5, 6, 7, 8, 9]; var removebleArray = [3,4,5]; var newNumberArray = numberArray.filter(m => { return !removebleArray.includes(m); }); console.log("new Array, [3,4,5] removed", newNumberArray);

function removeAll(array, key){ var index = array.indexOf(key); if(index === -1) return; array.splice(index, 1); removeAll(array,key); }

Array.prototype.removeAll = function(key){ var index = this.indexOf(key); if(index === -1) return; this.splice(index, 1); this.removeAll(key); }

function arrayWithout(arr, values) { var isArray = function(canBeArray) { if (Array.isArray) { return Array.isArray(canBeArray); } return Object.prototype.toString.call(canBeArray) === '[object Array]'; }; var excludedValues = (isArray(values)) ? values : [].slice.call(arguments, 1); var arrCopy = arr.slice(0); for (var i = arrCopy.length - 1; i >= 0; i--) { if (excludedValues.indexOf(arrCopy[i]) > -1) { arrCopy.splice(i, 1); } } return arrCopy; }

const arrayWithoutFastest = (() => { const isArray = canBeArray => ('isArray' in Array) ? Array.isArray(canBeArray) : Object.prototype.toString.call(canBeArray) === '[object Array]'; let mapIncludes = (map, key) => map.has(key); let objectIncludes = (obj, key) => key in obj; let includes; function arrayWithoutFastest(arr, ...thisArgs) { let withoutValues = isArray(thisArgs[0]) ? thisArgs[0] : thisArgs; if (typeof Map !== 'undefined') { withoutValues = withoutValues.reduce((map, value) => map.set(value, value), new Map()); includes = mapIncludes; } else { withoutValues = withoutValues.reduce((map, value) => { map[value] = value; return map; } , {}); includes = objectIncludes; } const arrCopy = []; const length = arr.length; for (let i = 0; i < length; i++) { // If value is not in exclude list if (!includes(withoutValues, arr[i])) { arrCopy.push(arr[i]); } } return arrCopy; } return arrayWithoutFastest; })();

const arr = [1,2,3,4,5,"name", false]; arrayWithoutFastest(arr, 1); // will return array [2,3,4,5,"name", false] arrayWithoutFastest(arr, 'name'); // will return [2,3,4,5, false] arrayWithoutFastest(arr, false); // will return [2,3,4,5] arrayWithoutFastest(arr,[1,2]); // will return [3,4,5,"name", false]; arrayWithoutFastest(arr, {bar: "foo"}); // will return the same array (new copy)

var myElement = "chocolate"; var myArray = ['chocolate', 'poptart', 'poptart', 'poptart', 'chocolate', 'poptart', 'poptart', 'chocolate']; /* Important code */ for (var i = myArray.length - 1; i >= 0; i--) { if (myArray[i] == myElement) myArray.splice(i, 1); }