var arr = [ 4, "Pete", "test", 8, "John", "", "test" ];
How can i remove from this array values test and empty string? How is the best method for this?
var arr = [ 4, "Pete", "test", 8, "John", "", "test" ];
How can i remove from this array values test and empty string? How is the best method for this?
var arr = [ 4, "Pete", "test", 8, "John", "", "test" ];
var l = arr.length;
while( l-- ) {
if( arr[l] === "test" || arr[l] === "" ) {
arr.splice(l, 1);
}
}
//> arr
//[4, "Pete", 8, "John"]
Alternative: filter
var arr = [ 4, "Pete", "test", 8, "John", "", "test" ]
.filter(function(v){return String(v).length && v !== 'test';});
//=> arr = [4, "Pete", 8, "John"];
if you know the index of your item in array you can easily use splice like
arr.splice(index,howmany)
howmany=The number of items to be removed