Simple function to clean an array if it has null or empty values, so if we have:
[ 'click1', 'click2', null, '', '', 'submitForm' ]
...it will return:
[ 'click1', 'click2', 'submitForm' ]
Here is my code:
function squeakyClean(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == null || arr[i] == '') {
arr.splice(i);
};
};
return arr;
}
I have a for loop to check each value in the array and an if statement to see if it is equal to null or and empty string, if so I used array splice method to remove the value and then return the clean array outside the for loop.
It works if you enter an array with no empty strings or null values, but if I enter [ 1, , 2, 3, 0, -1, 1.1 ]
it returns [1]
which it should not do. What am I missing here?
PS: I have looked how other people solved this using without a for loop and splice method, but I am interested in how to solve it using these two.
Simple function to clean an array if it has null or empty values, so if we have:
[ 'click1', 'click2', null, '', '', 'submitForm' ]
...it will return:
[ 'click1', 'click2', 'submitForm' ]
Here is my code:
function squeakyClean(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == null || arr[i] == '') {
arr.splice(i);
};
};
return arr;
}
I have a for loop to check each value in the array and an if statement to see if it is equal to null or and empty string, if so I used array splice method to remove the value and then return the clean array outside the for loop.
It works if you enter an array with no empty strings or null values, but if I enter [ 1, , 2, 3, 0, -1, 1.1 ]
it returns [1]
which it should not do. What am I missing here?
PS: I have looked how other people solved this using without a for loop and splice method, but I am interested in how to solve it using these two.
Share Improve this question edited Jul 5, 2015 at 18:56 Jordan Running 106k18 gold badges188 silver badges187 bronze badges asked Jul 5, 2015 at 18:44 Anton KastritskiyAnton Kastritskiy 1,3431 gold badge12 silver badges24 bronze badges 2- Maybe this is just a typo, but: If you enter an array like [ 1, , 3, 4 ] the value at index 1 of the array (the second value) will neither be null nor an empty string, but undefined. If you don't want that, you need to filter it out with (typeof arr[i]==='undefined'). – RhinoDevel Commented Jul 5, 2015 at 18:54
- That is true, I didn't think about it. But once I changed arr.splice(i) to arr.splice(i, 1) it fully works. Hmm – Anton Kastritskiy Commented Jul 5, 2015 at 18:59
5 Answers
Reset to default 3Consider using filter
. For example, the following will filter out null
, undefined
, false
, +0
, -0
, NaN
and ""
:
arr.filter(Boolean)
Your code is fine apart from your use of the .splice
method where you must also specify the number of items on from this index to delete.
Example: Array.splice(index, numberOfItemsFromIndex);
Therefore to fix your code it should be as simple as:
function squeakyClean(arr) {
for (var i = 0; i < arr.length; i++) {
if (arr[i] == null || arr[i] == '') {
arr.splice(i, 1);
};
};
return arr;
}
(Splice Documentation)
Without using splice or other functions, you can swipe all of the empty values through another array like this:
function swipeArray(arr) {
var newArr = new Array();
for (var i = 0; i < arr.length; i++) {
if (arr[i]||arr[i]===0||arr[i]===false)
newArr.push(arr[i]);
}
return newArr;
}
var arr = new Array('click1', 'click2', null, '', '', 'submitForm',0,false);
document.getElementById("out").textContent = JSON.stringify(swipeArray(arr))
<p id="out"></p>
Edit: if you extend the if condition, you can avoid cleaning 0
, false
or other values in the array.
Your code has two problems:
- You must pass a 2nd argument to
splice
in order to tell it how many items you want to delete - Deletions with
splice
reindex the array. That means you will skip an item when you remove the previous one. You can solve it iterating backwards.
function squeakyClean(arr) {
for (var i = arr.length-1; i >= 0 ; --i)
if (arr[i] == null || arr[i] == '')
arr.splice(i, 1);
return arr;
}
alert(
[ 'click1', 'click2', null, '', '', 'submitForm' ].filter(function(x){
return !!x; // or simple `return x;`
})
)