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

Incredible Demos

  • By
    Create Custom Events in MooTools 1.2

    Javascript has a number of native events like "mouseover," "mouseout", "click", and so on. What if you want to create your own events though? Creating events using MooTools is as easy as it gets. The MooTools JavaScript What's great about creating custom events in MooTools is...

  • By
    Select Dropdowns, MooTools, and CSS Print

    I know I've harped on this over and over again but it's important to enhance pages for print. You can do some things using simple CSS but today's post features MooTools and jQuery. We'll be taking the options of a SELECT element and generating...

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!