I have an array like:
A = ['a', 'del', 'b', 'del', 'c']
how can i remove the elements del such that the result is,
B = ['a', 'b', 'c']
I tried the pop and indexOf method but was unable
I have an array like:
A = ['a', 'del', 'b', 'del', 'c']
how can i remove the elements del such that the result is,
B = ['a', 'b', 'c']
I tried the pop and indexOf method but was unable
Use filter()
for filtering elements from an array
var A = ['a', 'del', 'b', 'del', 'c'];
var B = A.filter(function(v) {
return v != 'del';
});
console.log(B);
For older browser check polyfill option of filter method.
In case if you want to remove element from existing array then use splice()
with a for loop
var A = ['a', 'del', 'b', 'del', 'c'];
for (var i = 0; i < A.length; i++) {
if (A[i] == 'del') {
A.splice(i, 1); // remove the eleemnt from array
i--; // decrement i since one one eleemnt removed from the array
}
}
console.log(A);