9951 explained code solutions for 126 technologies


javascriptRemove empty elements from an array


var arr = [1, 2, 3, null, , , undefined, 4, 5, ''];
var clean = arr.filter(function (v) { return v != null && v != ''; });ctrl + c
var arr

declare test array with several empty elements to remove

clean

will contain resulting array with non-empty values only

return v != null && v != ''

remove null/undefined and empty strings from array