Categories
JavaScript Answers

How to use JavaScript array map to filter results with if conditional?

Spread the love

Sometimes, we want to use JavaScript array map to filter results with if conditional.

In this article, we’ll look at how to use JavaScript array map to filter results with if conditional.

How to use JavaScript array map to filter results with if conditional?

To use JavaScript array map to filter results with if conditional, we can use the array filter before map.

For instance, we write

const appIds = applicationsHere
  .filter((obj) => {
    return obj.selected;
  })
  .map((obj) => {
    return obj.id;
  });

to call filter to return an array with all the items with selected set to true.

Then we call map with a callback to return the id property of each object and put them in a new array and return the array.

Conclusion

To use JavaScript array map to filter results with if conditional, we can use the array filter before map.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.