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
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

  • By
    An Interview with Eric Meyer

    Your early CSS books were instrumental in pushing my love for front end technologies. What was it about CSS that you fell in love with and drove you to write about it? At first blush, it was the simplicity of it as compared to the table-and-spacer...

Incredible Demos

  • By
    CSS @supports

    Feature detection via JavaScript is a client side best practice and for all the right reasons, but unfortunately that same functionality hasn't been available within CSS.  What we end up doing is repeating the same properties multiple times with each browser prefix.  Yuck.  Another thing we...

  • By
    Multiple Backgrounds with CSS

    Anyone that's been in the web development industry for 5+ years knows that there are certain features that we should have had several years ago. One of those features is the HTML5 placeholder; we used JavaScript shims for a decade before placeholder came...

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!