1

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

3
  • Are your all lines separated by a "\n character"? Commented Jul 5, 2016 at 16:20
  • 3
    show the actual array content Commented Jul 5, 2016 at 16:36
  • 1
    Are you asking how to delete an element in an array? Array.splice is probably what you are looking for. Commented Jul 5, 2016 at 17:00

1 Answer 1

2

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("") != ""});
Sign up to request clarification or add additional context in comments.

5 Comments

Yes all lines are seperated by a '\n' since it is copied from a excel.I was able to parse and add values into array. Now want to remove empty record. The record '7 8 9' should come at index 2 now.
@Kevin: I get an error if I use var result = input.filter(element => element.join("") != ""); Error is:element not defined
Strange Justin, would we have different interpretor ? That works perfectly on my plateform. What plateform do you test on ?
I am using eclipse Mars on windows7.
I also use Eclipse Mars with the plugin ENIDE for Node.js and that works fine. but you can write this instead var result = input.filter(function(element) {return element.join("") != ""});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.