Sknoe / Notes
JavaScript Array Methods: map(), filter(), find(), and reduce() Explained
Four familiar methods, four different results. Learn what each one returns and how to choose the right tool for your data.
You open a component and see products.filter(...).map(...). Then another file uses find(), and somewhere a reduce() is adding up a basket total. They all take a callback, so it is tempting to think they do roughly the same thing.
They do not. Each method asks a different question about an array and returns a different kind of result. Once you know what comes back, the method names become easier to choose—and easier to read in React code.
We will use the same small product list throughout. You do not need React to understand the first examples.
What data are we working with?
const products = [{ id: 'sku-101', name: 'Trail Backpack', price: 64, inStock: true },{ id: 'sku-102', name: 'Rain Shell', price: 89, inStock: false },{ id: 'sku-103', name: 'Trail Shoes', price: 72, inStock: true },];
products is an array. Each item is an object describing one product. The examples below pass a function—called a callback—to an array method. The method calls that function for the items it processes.
What does map() return?
Use map() when you want to make a new value for every array item. It returns a new array with the same number of items as the original.
const productNames = products.map((product) => product.name);console.log(productNames);// ['Trail Backpack', 'Rain Shell', 'Trail Shoes']
For each product, map() calls the callback and puts its returned value in the new array. Here, the callback returns a name, so the result is an array of strings.
Think: one input item becomes one output item. If your callback does not return a value, the corresponding output is undefined—a common reason a map() result looks empty or wrong.
What does filter() return?
Use filter() when you want to keep only the items that pass a test. It returns a new array, which may have fewer items—or none.
const availableProducts = products.filter((product) => product.inStock);console.log(availableProducts.map((product) => product.name));// ['Trail Backpack', 'Trail Shoes']
The callback returns a truthy or falsy result for each item. filter() keeps the items whose test is truthy. Notice the result is still an array, even if only one product matches.
Think: which items should stay?
What does find() return?
Use find() when you want the first item that passes a test. It returns that item itself—not an array. If nothing matches, it returns undefined.
const backpack = products.find((product) => product.id === 'sku-101');const missingProduct = products.find((product) => product.id === 'sku-999');console.log(backpack.name); // 'Trail Backpack'console.log(missingProduct); // undefined
Because find() might return undefined, check that a match exists before reading its properties:
if (backpack) {console.log(backpack.price);}
Think: which first item matches? If you need every match, use filter() instead.
What does reduce() return?
Use reduce() when you want to combine array items into one accumulated result. That result could be a number, string, object, or another array.
For example, we can add the prices of the available products:
const availableTotal = products.filter((product) => product.inStock).reduce((total, product) => total + product.price, 0);console.log(availableTotal); // 136
reduce() calls its callback with an accumulator and the current item. The callback returns the next accumulator value. The final accumulator is the result.
Here, total starts at 0 because 0 is the second argument to reduce(). The first available product changes it to 64; the next changes it to 136. Providing an initial value makes the behavior clear and also handles an empty array: reducing an empty array from 0 returns 0.
Think: what single result am I building from these items? If a reduce() callback is hard to follow, a plain loop may be clearer. You do not need to use reduce() just because it exists.
How do I choose between them?
Ask what shape you need next:
- Need one output for every item? Use `map()`; you get an array.
- Need only the items that pass a test? Use `filter()`; you get an array.
- Need the first matching item? Use `find()`; you get an item or undefined.
- Need to combine the items into one result? Use `reduce()`; you get the accumulated value.
The question is not “Which method looks shortest?” It is “What should the next line receive?”
How do these methods show up in React?
React commonly uses map() to describe one UI element for each item in a list. The array method is still ordinary JavaScript: it builds an array of results. React then renders those returned elements.
function ProductList({ products }) {const availableProducts = products.filter((product) => product.inStock);return (<ul>{availableProducts.map((product) => (<li key={product.id}>{product.name}: ${product.price}</li>))}</ul>);}
Here filter() decides which products to include, and map() makes an <li> description for each one. The key helps React match list items between renders; it does not change what map() returns.
One small thing: these methods do not all change the original
map(), filter(), and reduce() do not mutate the array they are called on; they return a result. Their callbacks can still mutate objects if you write them to do that. Also, the methods do not automatically make deep copies of objects inside an array. find() returns the original matching item, so changing a property on that returned object changes the object in the original array too.
If you are updating data, make sure you understand whether you are creating new objects too. In JavaScript Immutability: Change the Data Without Breaking the Original, we follow that distinction into React state.
Want the steps before React?
This guide follows JavaScript Before React: A Step-by-Step Guide to the Fundamentals. For object syntax used in the examples, see JavaScript Destructuring and Spread: React and API Examples. When these pieces feel comfortable, continue to React for Salesforce Storefront Next: The Concepts SFRA Developers Need.
Did this click?
Tap the bulb if this explanation helped.