JavaScript Array Methods
Array methods like map, filter, and reduce are the backbone of modern JavaScript. They replace verbose for loops with declarative, readable one-liners. Essential for every JS interview.
map() — Transform Every Element
const numbers = [1, 2, 3, 4, 5];
// Double every number
const doubled = numbers.map(n => n * 2);
// [2, 4, 6, 8, 10]
// Extract property from objects
const users = [{id:1,name:'Rahul'},{id:2,name:'Priya'}];
const names = users.map(u => u.name);
// ['Rahul', 'Priya']
// Transform with index
const indexed = numbers.map((n, i) => `${i}: ${n}`);
// ['0: 1', '1: 2', ...]
filter() — Keep Matching Elements
const numbers = [1, 2, 3, 4, 5, 6];
// Keep evens
const evens = numbers.filter(n => n % 2 === 0);
// [2, 4, 6]
// Filter objects
const users = [
{name:'Rahul', active:true},
{name:'Priya', active:false},
{name:'Amit', active:true},
];
const activeUsers = users.filter(u => u.active);
// [{name:'Rahul',...}, {name:'Amit',...}]
reduce() — Collapse to Single Value
const numbers = [1, 2, 3, 4, 5];
// Sum
const sum = numbers.reduce((acc, n) => acc + n, 0); // 15
// Build object from array
const users = [{id:1,name:'Rahul'},{id:2,name:'Priya'}];
const byId = users.reduce((acc, u) => {
acc[u.id] = u;
return acc;
}, {});
// {1: {id:1,name:'Rahul'}, 2: {id:2,name:'Priya'}}
// Flatten nested arrays
const nested = [[1,2],[3,4],[5]];
const flat = nested.reduce((acc, arr) => acc.concat(arr), []);
// [1, 2, 3, 4, 5] — or just use nested.flat()
find(), findIndex(), every(), some()
const users = [{id:1,name:'Rahul',age:25},{id:2,name:'Priya',age:30},{id:3,name:'Amit',age:22}];
// find — returns first match (or undefined)
const priya = users.find(u => u.name === 'Priya');
// {id:2, name:'Priya', age:30}
// findIndex — returns index (or -1)
const idx = users.findIndex(u => u.age < 25);
// 2 (Amit at index 2)
// every — true if ALL match
const allAdults = users.every(u => u.age >= 18); // true
// some — true if ANY match
const hasMinor = users.some(u => u.age < 18); // false
// includes — check value exists
[1,2,3].includes(2); // true
flat() and flatMap()
// flat — flatten nested arrays
const nested = [1, [2, 3], [4, [5, 6]]];
nested.flat(); // [1, 2, 3, 4, [5, 6]]
nested.flat(2); // [1, 2, 3, 4, 5, 6]
// flatMap — map then flatten one level
const sentences = ['Hello World', 'Foo Bar'];
const words = sentences.flatMap(s => s.split(' '));
// ['Hello', 'World', 'Foo', 'Bar']
Q: Does map() mutate the original array?
No. map(), filter(), and reduce() all return new arrays/values without modifying the original. This is what makes them safe for functional programming patterns. forEach() also does not return a value — use it only for side effects.
Q: When would you use reduce() instead of map() + filter()?
reduce() is more powerful — it can implement both map and filter, and handle cases where the output type differs from the input (array to object, array to number). Use it when you need to transform an array into a completely different structure.
Comments (0)
No comments yet. Be the first!
Leave a Comment