1var colors = ["red","blue","car","green"];
2var carIndex = colors.indexOf("car");//get "car" index
3//remove car from the colors array
4colors.splice(carIndex, 1); // colors = ["red","blue","green"]
1var data = [1, 2, 3];
2
3// remove a specific value
4// splice(starting index, how many values to remove);
5data = data.splice(1, 1);
6// data = [1, 3];
7
8// remove last element
9data = data.pop();
10// data = [1, 2];
1var colors = ["red","blue","car","green"];
2var carIndex = colors.indexOf("car");//get "car" index
3//remove car from the colors array
4colors.splice(carIndex, 1); // colors = ["red","blue","green"]
1// Remove single item
2function removeItemOnce(arr, value) {
3 var index = arr.indexOf(value);
4 if (index > -1) {
5 arr.splice(index, 1);
6 }
7 return arr;
8}
9
10// Remove all items
11function removeItemAll(arr, value) {
12 var i = 0;
13 while (i < arr.length) {
14 if (arr[i] === value) {
15 arr.splice(i, 1);
16 } else {
17 ++i;
18 }
19 }
20 return arr;
21}
22
23// Usage
24console.log(removeItemOnce([2, 5, 9, 1, 5, 8, 5], 5));
25console.log(removeItemAll([2, 5, 9, 1, 5, 8, 5], 5));
1let forDeletion = [2, 3, 5]
2
3let arr = [1, 2, 3, 4, 5, 3]
4
5arr = arr.filter(item => !forDeletion.includes(item))
6// !!! Read below about array.includes(...) support !!!
7
8console.log(arr)
9// [ 1, 4 ]
1var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];var removed = arr.splice(2,2);/*removed === [3, 4]arr === [1, 2, 5, 6, 7, 8, 9, 0]*/