Remove empty or whitespace strings from array - Javascript

45,098

Solution 1

filter works, but you need the right predicate function, which Boolean isn't (for this purpose):

// Example 1 - Using String#trim (added in ES2015, needs polyfilling in outdated
// environments like IE)
arr = arr.filter(function(entry) { return entry.trim() != ''; });

or

// Example 2 - Using a regular expression instead of String#trim
arr = arr.filter(function(entry) { return /\S/.test(entry); });

(\S means "a non-whitespace character," so /\S/.test(...) checks if a string contains at least one non-whitespace char.)

or (perhaps a bit overboard and harder to read)

// Example 3
var rex = /\S/;
arr = arr.filter(rex.test.bind(rex));

With an ES2015 (aka ES6) arrow function, that's even more concise:

// Example 4
arr = arr.filter(entry => entry.trim() != '');

or

// Example 5
arr = arr.filter(entry => /\S/.test(entry));

Live Examples -- The ES5 and earlier ones:

var arr = ['Apple', '  ', 'Mango', '', 'Banana', ' ', 'Strawberry'];
console.log("Example 1: " + JSON.stringify(arr.filter(function(entry) { return entry.trim() != ''; })));
console.log("Example 2: " + JSON.stringify(arr.filter(function(entry) { return /\S/.test(entry); })));
var rex = /\S/;
console.log("Example 3: " + JSON.stringify(arr.filter(rex.test.bind(rex))));

...and the ES2015 (ES6) ones (won't work if your browser doesn't support arrow functions yet):

var arr = ['Apple', '  ', 'Mango', '', 'Banana', ' ', 'Strawberry'];
console.log("Example 4: " + JSON.stringify(arr.filter(entry => !entry.trim() == '')));
console.log("Example 5: " + JSON.stringify(arr.filter(entry => /\S/.test(entry))));

Solution 2

You can take advantage of empty string as falsy value.

You can use Array#filter with String#trim.

Using ES6 Arrow function:

arr = arr.filter(e => String(e).trim());

var arr = ['Apple', '  ', 'Mango', '', 'Banana', ' ', 'Strawberry'];
var nonEmpty = arr.filter(e => String(e).trim());

document.getElementById('result').innerHTML = JSON.stringify(nonEmpty, 0, 4);
<pre id="result"></pre>

Using ES5 anonymous function:

arr = arr.filter(function(e) {
    return String(e).trim();
});

var arr = ['Apple', '  ', 'Mango', '', 'Banana', ' ', 'Strawberry'];
var nonEmpty = arr.filter(function(e) {
    return String(e).trim();
});

document.getElementById('result').innerHTML = JSON.stringify(nonEmpty, 0, 4);
<pre id="result"></pre>

Solution 3

Based on this MDN reference:

\s
Matches a single white space character, including space, tab, form feed, line feed and other Unicode spaces. Equivalent to [ \f\n\r\t\v​\u00a0\u1680​\u180e\u2000​-\u200a​\u2028\u2029\u202f\u205f​\u3000\ufeff].

And on ECMA 262 reference, saying \s should match "White Space" like \u0009 (Tab, <TAB>), \u000B (Vertical Tab, <VT>), \u000C (Form Feed, <FF>), \u0020 (Space, <SP>), \u00A0 (No-break space, <NBSP>), \uFEFF (Byte Order Mark, <BOM>), and other category “Zs” (<USP>), and also "line terminators" like \u000A (Line Feed, <LF>), \u000D (Carriage Return, <CR>), \u2028 (Line separator, <LS>) and \u2029 (Paragraph separator, <PS>), you can use the following code to remove elements that are either empty or whitespace only if trim() is not natively available:

var arr = ['Apple', '  ', 'Mango', '', 'Banana', ' ', 'Strawberry'];
arr = arr.filter(s => s.replace(/\s+/g, '').length !== 0);
// Or for ES5
// arr = arr.filter(function (el) { return el.replace(/\s+/g, '').length !== 0; });
console.log(arr);

In case some old browsers behave differently with \s, replace it with [ \f\n\r\t\v​\u00a0\u1680​\u180e\u2000​-\u200a​\u2028\u2029\u202f\u205f​\u3000\ufeff] character class:

arr = arr.filter(function (el) { return el.replace(/[ \f\n\r\t\v​\u00a0\u1680​\u180e\u2000​-\u200a​\u2028\u2029\u202f\u205f​\u3000\ufeff]+/g, '').length !== 0; });

And you can also customize it further to include new Unicode spaces to come.

Share:
45,098
Daniel
Author by

Daniel

Updated on December 09, 2020

Comments

  • Daniel
    Daniel over 3 years

    I've found this beautiful method for removing empty strings - arr = arr.filter(Boolean).

    But it doesn't seem to work on whitespace strings.

    var arr = ['Apple', '  ', 'Mango', '', 'Banana', ' ', 'Strawberry'];
    arr = arr.filter(Boolean);
    // ["Apple", "  ", "Mango", "Banana", " ", "Strawberry"]
    
    // should be ["Apple", "Mango", "Banana", "Strawberry"]
    

    Is there a nice way to expand this method to removing whitespaces as well or should i trim the whitespaces by iterating the array first?