Justin
Justin

Reputation: 45

Remove empty row from array in javascript

I am trying to check and remove any empty row in my table mapped to an array in javascript.

Sample array would be like below.

1   2   3
4   5   6

7   8   9

Now this array is having 4 rows and 3rd one is empty which I want to remove. How to do that. I tried using if(adata[i]) is not null, but that wont work since an indivdual value can be null. i want to check for complete row being null

Upvotes: 1

Views: 1963

Answers (1)

kevin ternet
kevin ternet

Reputation: 4612

This short code should match your requirement :

var input = [[1,,3], [11,, ,12], [,,,,,], [37,38,39]];
var result = input.filter(element => element.join("") != "");
console.log(result); // [ [ 1, , 3 ], [ 11, , , 12 ], [ 37, 38, 39 ] ]

Edit : for ES5 you'll write rather

var result = input.filter(function(element) {return element.join("") != ""});

Upvotes: 2

Related Questions