Tip 23 Pull Out Subsets of Data with filter() and find()

In this tip, you’ll learn how to change the size of an array while retaining the shape of the items.

In the previous tip, you created a new array by pulling out only the relevant information from the original array. You’ll likely encounter situations where you want to keep the shape of the data, but you only want a subset of the total items. Maybe you only want users that live in a certain city, but you still need all their information. The array method filter() will perform this exact action. Unlike the map() method, you aren’t changing any information in the array—you’re just reducing what you get back.

As an example, let’s filter a simple array of strings. You have a team ...

Get Simplifying JavaScript now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.