Example 1: remove an element from array
var colors = ["red", "blue", "car","green"];
colors = colors.filter(data => data != "car");
colors = colors.filter(function(data) { return data != "car"});
Example 2: remove array js
var ar = [1, 2, 3, 4, 5, 6];ar.length = 4;
var ar = [1, 2, 3, 4, 5, 6];ar.pop();
var ar = ['zero', 'one', 'two', 'three'];ar.shift();
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];var removed = arr.splice(2,2);
["bar", "baz", "foo", "qux"]list.splice(0, 2)
var arr = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
for( var i = 0; i < arr.length; i++){
if ( arr[i] === 5) {
arr.splice(i, 1);
}
}
var arr = [1, 2, 3, 4, 5, 5, 6, 7, 8, 5, 9, 0];
for( var i = 0; i < arr.length; i++){
if ( arr[i] === 5) {
arr.splice(i, 1); i--;
}
}
var array = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
var filtered = array.filter(function(value, index, arr){
return value > 5;
});
var evens = _.remove(array, function(n) {
return n % 2 === 0;
});
console.log(array);
function arrayRemove(arr, value) {
return arr.filter(function(ele){
return ele != value; });
}
var result = arrayRemove(array, 6);
var ar = [1, 2, 3, 4, 5, 6];delete ar[4];
Example 3: javascript function to add or remove from array if
function removeItemOnce(arr, value) {
var index = arr.indexOf(value);
if (index > -1) {
arr.splice(index, 1);
}
return arr;
}
function removeItemAll(arr, value) {
var i = 0;
while (i < arr.length) {
if (arr[i] === value) {
arr.splice(i, 1);
} else {
++i;
}
}
return arr;
}
console.log(removeItemOnce([2,5,9,1,5,8,5], 5))
console.log(removeItemAll([2,5,9,1,5,8,5], 5))