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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.