How To Remove Empty Array Values ("") From An Array?
Solution 1:
You could use the filter like:
arr = arr.filter(item => item);
Example:
let arr = ['One', 'Two', '', 'Four', '', ''];
arr = arr.filter(item => item);
console.log(arr);
// Result// ['One', 'Two', 'Four']
Because an empty string evaluates to boolean false
.
It works with all falsy values like 0
, false
, null
, undefined
, ''
, etc.
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.
Has A Class, Add Class To Table Cell |
Let's say I have the following html: …
WebRTC Video Constraints Not Working
I'm trying to get a lower resolution from the webcam na…
HasClass Doesn't Work In My Js Code?
I want to use hasClass in the following code, but that does…
I am relatively new to Javascript/Ajax. When the user click…
Highcharts Donutchart: Avoid Showing Duplicate Legend With Nested Charts
I am trying to represent nested data using Highcharts Donut…
How Do I Use Data From Vue.js Child Component Within Parent Component?
I have a form component where I use a child component. I wa…
Logic For The Next Button For The Questionnaire?
I am beginner in AngularJS and facing some issues. I am try…
Assignment To Property Of Function Parameter (no-param-reassign)
I have this function and while I have this working nicely, …
I am making a web application using nodejs and angular cli …
How To Show Website Preloader Only Once
I added a preloader to my website and the preloader animati…
|
---|
Post a Comment for "How To Remove Empty Array Values ("") From An Array?"