JavaScript Array and Object Methods - The Essential Guide
January 22, 2026 • 8 min read
Think of array methods as assembly line workers in a factory. Each worker has a specific job: one transforms items (map), another filters out defective ones (filter), and another counts or combines everything (reduce). Instead of manually looping through each item, you tell these workers what to do, and they handle the repetitive work for you.
Let’s explore JavaScript’s most essential array and object methods that will transform how you work with data.
Transform with map
The map method creates a new array by transforming each element. Whatever you return from the callback becomes the new element.
const numbers = [1, 2, 3, 4, 5];
// Double each number
const doubled = numbers.map(num => num * 2);
console.log(doubled); // [2, 4, 6, 8, 10]
// Original array unchanged
console.log(numbers); // [1, 2, 3, 4, 5]
Common uses for map:
// Extract properties from objects
const users = [
{ name: "Alice", age: 25 },
{ name: "Bob", age: 30 },
{ name: "Charlie", age: 35 }
];
const names = users.map(user => user.name);
console.log(names); // ["Alice", "Bob", "Charlie"]
// Transform data shapes
const userCards = users.map(user => ({
displayName: user.name.toUpperCase(),
isAdult: user.age >= 18
}));
console.log(userCards);
// [{ displayName: "ALICE", isAdult: true }, ...]
// Access index if needed
const indexed = numbers.map((num, index) => `${index}: ${num}`);
console.log(indexed); // ["0: 1", "1: 2", "2: 3", "3: 4", "4: 5"]
Select with filter
The filter method creates a new array containing only elements that pass a test. Return true to keep an element, false to exclude it.
const numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
// Keep only even numbers
const evens = numbers.filter(num => num % 2 === 0);
console.log(evens); // [2, 4, 6, 8, 10]
// Keep numbers greater than 5
const big = numbers.filter(num => num > 5);
console.log(big); // [6, 7, 8, 9, 10]
Filtering objects:
const products = [
{ name: "Laptop", price: 999, inStock: true },
{ name: "Phone", price: 699, inStock: false },
{ name: "Tablet", price: 499, inStock: true },
{ name: "Watch", price: 299, inStock: true }
];
// In-stock items only
const available = products.filter(product => product.inStock);
console.log(available.length); // 3
// Affordable and available
const affordable = products.filter(
product => product.price < 500 && product.inStock
);
console.log(affordable);
// [{ name: "Tablet", ... }, { name: "Watch", ... }]
Accumulate with reduce
The reduce method combines all elements into a single value. It takes a callback with an accumulator and current element, plus an initial value.
const numbers = [1, 2, 3, 4, 5];
// Sum all numbers
const sum = numbers.reduce((accumulator, current) => {
return accumulator + current;
}, 0);
console.log(sum); // 15
// Shorter version
const total = numbers.reduce((acc, num) => acc + num, 0);
console.log(total); // 15
How reduce works step by step:
// reduce((acc, cur) => acc + cur, 0) with [1, 2, 3, 4, 5]
// Step 1: acc = 0, cur = 1 → returns 1
// Step 2: acc = 1, cur = 2 → returns 3
// Step 3: acc = 3, cur = 3 → returns 6
// Step 4: acc = 6, cur = 4 → returns 10
// Step 5: acc = 10, cur = 5 → returns 15
Powerful reduce patterns:
// Find maximum value
const max = numbers.reduce((a, b) => (a > b ? a : b));
console.log(max); // 5
// Count occurrences
const fruits = ["apple", "banana", "apple", "orange", "banana", "apple"];
const counts = fruits.reduce((acc, fruit) => {
acc[fruit] = (acc[fruit] || 0) + 1;
return acc;
}, {});
console.log(counts); // { apple: 3, banana: 2, orange: 1 }
// Group by property
const people = [
{ name: "Alice", department: "Engineering" },
{ name: "Bob", department: "Marketing" },
{ name: "Charlie", department: "Engineering" }
];
const byDepartment = people.reduce((acc, person) => {
const dept = person.department;
acc[dept] = acc[dept] || [];
acc[dept].push(person);
return acc;
}, {});
console.log(byDepartment);
// { Engineering: [{...}, {...}], Marketing: [{...}] }
Find elements with find
The find method returns the first element that passes a test, or undefined if none match.
const users = [
{ id: 1, name: "Alice", role: "admin" },
{ id: 2, name: "Bob", role: "user" },
{ id: 3, name: "Charlie", role: "user" }
];
// Find by ID
const user = users.find(u => u.id === 2);
console.log(user); // { id: 2, name: "Bob", role: "user" }
// Find first admin
const admin = users.find(u => u.role === "admin");
console.log(admin); // { id: 1, name: "Alice", role: "admin" }
// Not found returns undefined
const notFound = users.find(u => u.id === 999);
console.log(notFound); // undefined
find vs filter:
const numbers = [1, 2, 3, 4, 5, 4, 3];
// find: returns FIRST match (single element)
const firstFour = numbers.find(n => n === 4);
console.log(firstFour); // 4
// filter: returns ALL matches (array)
const allFours = numbers.filter(n => n === 4);
console.log(allFours); // [4, 4]
Test conditions with some and every
The some method returns true if at least one element passes the test. The every method returns true only if all elements pass.
const numbers = [1, 2, 3, 4, 5];
// some: at least one even?
console.log(numbers.some(n => n % 2 === 0)); // true
// every: all positive?
console.log(numbers.every(n => n > 0)); // true
// every: all even?
console.log(numbers.every(n => n % 2 === 0)); // false
Practical examples:
const users = [
{ name: "Alice", verified: true, age: 25 },
{ name: "Bob", verified: false, age: 17 },
{ name: "Charlie", verified: true, age: 30 }
];
// Has any unverified user?
const hasUnverified = users.some(u => !u.verified);
console.log(hasUnverified); // true
// Are all users adults?
const allAdults = users.every(u => u.age >= 18);
console.log(allAdults); // false
// Form validation
const formFields = [
{ name: "email", valid: true },
{ name: "password", valid: true },
{ name: "phone", valid: false }
];
const isFormValid = formFields.every(field => field.valid);
console.log(isFormValid); // false
Object.keys, values, and entries
These methods convert objects into arrays, making them iterable with array methods.
const user = {
name: "Alice",
age: 25,
city: "New York"
};
// Object.keys: array of property names
const keys = Object.keys(user);
console.log(keys); // ["name", "age", "city"]
// Object.values: array of property values
const values = Object.values(user);
console.log(values); // ["Alice", 25, "New York"]
// Object.entries: array of [key, value] pairs
const entries = Object.entries(user);
console.log(entries);
// [["name", "Alice"], ["age", 25], ["city", "New York"]]
Iterating over objects:
const prices = {
apple: 1.5,
banana: 0.75,
orange: 2.0
};
// Using Object.keys
Object.keys(prices).forEach(fruit => {
console.log(`${fruit}: $${prices[fruit]}`);
});
// Using Object.entries (cleaner)
Object.entries(prices).forEach(([fruit, price]) => {
console.log(`${fruit}: $${price}`);
});
// Transform object values
const discounted = Object.fromEntries(
Object.entries(prices).map(([fruit, price]) => [fruit, price * 0.9])
);
console.log(discounted);
// { apple: 1.35, banana: 0.675, orange: 1.8 }
Chaining methods together
The real power comes from combining these methods:
const orders = [
{ id: 1, product: "Laptop", price: 999, shipped: true },
{ id: 2, product: "Phone", price: 699, shipped: false },
{ id: 3, product: "Tablet", price: 499, shipped: true },
{ id: 4, product: "Watch", price: 299, shipped: true }
];
// Get total revenue from shipped orders
const shippedRevenue = orders
.filter(order => order.shipped)
.map(order => order.price)
.reduce((sum, price) => sum + price, 0);
console.log(shippedRevenue); // 1797
// Get names of shipped products over $300
const expensiveShipped = orders
.filter(order => order.shipped && order.price > 300)
.map(order => order.product);
console.log(expensiveShipped); // ["Laptop", "Tablet"]
Common pitfalls
1. Forgetting the initial value in reduce:
const numbers = [1, 2, 3];
// Without initial value, first element becomes accumulator
const sum = numbers.reduce((a, b) => a + b); // Works: 6
// Empty array without initial value throws error!
const empty = [];
// empty.reduce((a, b) => a + b); // TypeError!
empty.reduce((a, b) => a + b, 0); // Safe: 0
2. Returning undefined from map:
const numbers = [1, 2, 3];
// Forgetting to return creates undefined values
const bad = numbers.map(n => {
n * 2; // Missing return!
});
console.log(bad); // [undefined, undefined, undefined]
// Fix: add return or use concise arrow
const good = numbers.map(n => n * 2);
console.log(good); // [2, 4, 6]
3. Using find when you need filter:
const numbers = [1, 2, 3, 4, 5, 4, 3];
// find only returns first match
const result = numbers.find(n => n > 3);
console.log(result); // 4 (just one value)
// Use filter for all matches
const results = numbers.filter(n => n > 3);
console.log(results); // [4, 5, 4]
Takeaways
- map: Transform each element into something new
- filter: Keep only elements that pass a test
- reduce: Combine all elements into a single value
- find: Get the first matching element
- some: Check if at least one element passes
- every: Check if all elements pass
- Object.keys/values/entries: Convert objects to arrays for iteration
These methods are the foundation of functional JavaScript. They make your code more readable, maintainable, and less prone to bugs than manual loops. Start using them in your projects, and you’ll wonder how you ever lived without them.
Frequently Asked Questions
What's the difference between map and forEach?
Both iterate over arrays, but map returns a new array with transformed values, while forEach returns undefined. Use map when you need to transform data, and forEach when you just need to perform side effects like logging.
When should I use reduce instead of a for loop?
Use reduce when you need to accumulate array values into a single result (sum, product, object, etc.). It's more declarative and functional, though for loops can be clearer for complex logic with multiple conditions.
What's the difference between find and filter?
find returns the first matching element (or undefined), while filter returns an array of ALL matching elements (or empty array). Use find when you need one result, filter when you need all matches.
Can I chain these array methods together?
Yes! Since map, filter, and other methods return arrays, you can chain them like `arr.filter(...).map(...).reduce(...)`. This creates clean, readable data transformation pipelines.
Do these methods mutate the original array?
No. Methods like map, filter, reduce, find, some, and every do NOT mutate the original array. They return new values while leaving the original unchanged, making them safe for functional programming patterns.
How do Object.entries differ from Object.keys and Object.values?
Object.keys returns an array of property names, Object.values returns an array of property values, and Object.entries returns an array of [key, value] pairs. Use entries when you need both keys and values together.
Stay in the loop
Get the latest updates on my blog, projects, and tech insights delivered straight to your inbox. No spam, unsubscribe anytime.
By subscribing, you agree to receive occasional updates. Unsubscribe anytime.