I have an two dimensional array, generated from a html table with jQuery, but some values are empty so '' is displayed. How can I remove the empty values?
If you want to keep some values like number 0 (zero) you could use item !== undefined. This filters only undefined values. Keep in mind to trim your string or check with regex to ensure empty strings without whitespaces.
Solution 2:
Try filtering with the Boolean function:
columns.filter(Boolean)
This will filter out all falsy values
Solution 3:
It's because when you columns[0].splice(i, 1); you are changing the same array you are iterating over so you might want to use an array filter like
columns[0] = columns[0].filter((val) => val != "");
instead of the for loop
Solution 4:
after creating the columns array,
filter the empty values like that
columns = columns.filter((v) => v != '')
Solution 5:
Just use filter function:-
columns = columns.filter(col => col);
It will remove empty values.
Share
Post a Comment
for "How To Remove Empty Array Values ("") From An Array?"
Post a Comment for "How To Remove Empty Array Values ("") From An Array?"