Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<h2>Simple ways:</h2> <pre><code>var arr = [1,2,,3,,-3,null,,0,,undefined,4,,4,,5,,6,,,,]; arr.filter(n =&gt; n) // [1, 2, 3, -3, 4, 4, 5, 6] arr.filter(Number) // [1, 2, 3, -3, 4, 4, 5, 6] arr.filter(Boolean) // [1, 2, 3, -3, 4, 4, 5, 6] </code></pre> <p><strong>or - (only for <em>single</em> array items of type "text")</strong></p> <pre><code>['','1','2',3,,'4',,undefined,,,'5'].join('').split(''); // output: ["1","2","3","4","5"] </code></pre> <p><strong>or - Classic way: simple iteration</strong></p> <pre><code>var arr = [1,2,null, undefined,3,,3,,,0,,,[],,{},,5,,6,,,,], len = arr.length, i; for(i = 0; i &lt; len; i++ ) arr[i] &amp;&amp; arr.push(arr[i]); // copy non-empty values to the end of the array arr.splice(0 , len); // cut the array and leave only the non-empty values arr // [1,2,3,3,[],Object{},5,6] </code></pre> <p><br/></p> <h2>via jQuery:</h2> <pre><code>var arr = [1,2,,3,,3,,,0,,,4,,4,,5,,6,,,,]; arr = $.grep(arr,function(n){ return n == 0 || n }); arr // [1, 2, 3, 3, 0, 4, 4, 5, 6] </code></pre> <p><br/></p> <h2>UPDATE - just another fast, cool way (using ES6):</h2> <pre><code>var arr = [1,2,null, undefined,3,,3,,,0,,,4,,4,,5,,6,,,,], temp = []; for(let i of arr) i &amp;&amp; temp.push(i); // copy each non-empty value to the 'temp' array arr = temp; delete temp; // discard the variable arr // [1, 2, 3, 3, 4, 4, 5, 6] </code></pre> <h3>Remove empty values</h3> <pre><code>['foo', '',,,'',,null, ' ', 3, true, [], [1], {}, undefined, ()=&gt;{}].filter(String) // ["foo", null, " ", 3, true, [1], Object {}, undefined, ()=&gt;{}] </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload