Array and Boolean

By  on  

One of the annoyances of old-school JavaScript was side effects; then Array.prototype got methods like filter, map, and forEach so we didn't need to burn variables before looping over values.  I can't explain how happy I am that the JavaScript language continues to evolve.

Every once in a while I need to filter an array by not its original value but instead a new value, so I use map:

myArray.map(item => {
    // Do whatever processing...

    // If we don't care about the item, return false or null or undefined
    return false;
});

While I get the new values I want, sometimes if an iteration returns a result I don't want, I return null or false, which is great, but then I'm left with a bunch of useless items in the resulting array.  The next step is using filter, in which case I could do:

myArray
    .map(item => {
        // ...
    })
    // Get rid of bad values
    .filter(item => item);

Since the values I don't want aren't truthy, the filter above removes those bad items.  Did you know there's a clearer way with Boolean?

myArray
    .map(item => {
        // ...
    })
    // Get rid of bad values
    .filter(Boolean);

If the value isn't truthy the item is filtered out and I'm left with only the items I want!

Recent Features

  • By
    Chris Coyier’s Favorite CodePen Demos

    David asked me if I'd be up for a guest post picking out some of my favorite Pens from CodePen. A daunting task! There are so many! I managed to pick a few though that have blown me away over the past few months. If you...

  • By
    6 Things You Didn’t Know About Firefox OS

    Firefox OS is all over the tech news and for good reason:  Mozilla's finally given web developers the platform that they need to create apps the way they've been creating them for years -- with CSS, HTML, and JavaScript.  Firefox OS has been rapidly improving...

Incredible Demos

Discussion

  1. Alec

    This is definitely good to know. I don’t think the people I work with are familiar enough with Boolean as a function. If you’ve never used it before, at first glance it might look like it returns values where typeof item === 'boolean', thus true or false would pass. I think clearest enough is just a reusable fn name like "isTruthy". I like how you do the map, then filter. I find myself doing that a lot. Especially where you might map to a new value, but that value might not always exist. I’ve seen this done using reduce or loops, and I think this approach makes the logic incredibly clear.

  2. bryanyang

    Wow, I use the fliter many times. but also don’t know the Boolen can kick out the bad item.

  3. MaxArt

    > Every once in a while I need to filter an array by not its original value but instead a new value, so I use map

    I feel a more specific example is needed, otherwise I’d say you’re usually good with just filter. Maybe you mean you actually mean that you need to map the values anyway, but I’d say you should filter first, and then map, so you can limit the array size beforehand.

    Also it’s nice to know that the upcoming stage 3 proposal flatMap (available on Chrome 69 and Firefox 62, that got just released!) can be used to do a map and a filter in one go! Just return an empty array for those elements you want to be left out.

  4. Zac

    I would definitely recommend just using a reduce for this with a list as your accumulator. That way you only have to loop over the array once.

Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!