javascript - Find and remove element from array -
if have javascript array of numbers
[1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1] and want search through array , remove particular number 4 giving me
[1, 2, 5, 7, 5, 7, 9, 2, 1] what's best way that
i thinking might like
for(var = 0; < myarray.length; i++) { if(myarray[i] == 4) { myarray.remove(i) } } but there no remove function array. if remove element array messes i unless correct it.
you can use .splice() remove 1 or more items array , if iterate front of array, indexing doesn't messed when remove item.
var arr = [1, 2, 5, 7, 5, 4, 7, 9, 2, 4, 1]; (var = arr.length - 1; >= 0; i--) { if (arr[i] == 4) { arr.splice(i, 1); } }
Comments
Post a Comment