I have resultant
array
like this,
[["apple","banana"],[],[],[],["kiwi"],[],[]]
How can I remove all the empty []
array
so that my resultant
array
would look like this:
[["apple","banana"],["kiwi"]];
var array = [["apple","banana"],[],[],[],["kiwi"],[],[]];// sample array
Use Array.prototype.filter
to filter out the empty arrays - see demo below:
var array = [["apple","banana"],[],[],[],["kiwi"],[],[]];// sample array
var result = array.filter(e => e.length);
console.log(result);
In ES5, you can omit the arrow function used above:
var array = [["apple","banana"],[],[],[],["kiwi"],[],[]];// sample array
var result = array.filter(function(e) {
return e.length;
});
console.log(result);
The ES5 compatible code for @kukuz answer.
var array = [["apple","banana"],[],[],[],["kiwi"],[],[]];// sample array
var result = array.filter(function(x){return x.length});
console.log(result);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With