Core Array Methods
Core Array Methods
];
map()
map() is a JavaScript array method used to transform each item in an array and return a new
array with the same length.
Syntax:
});
console.log(names);
📍In React:
{students.map(s => <p key={s.id}>{s.name}</p>)}
forEach() is a method used to run a function for each item in an array. It is mainly used to loop
through arrays and perform some action (like console.log, update, etc.).
// do something
});
numbers.forEach((num) => {
console.log(num * 2);
});
const arr = [10, 20, 30];
console.log(result1); // undefined
What is filter()?
filter() is a method that lets you pick specific items from an array based on a condition.
It returns a new array containing only the items that satisfy the condition.
Syntax:
return condition;
});
s.name.toLowerCase().includes(searchTerm.toLowerCase())
);
return (
<ul>
</ul>
);
In React:
return (
<ul>
</ul>
);
What is reduce()?
reduce() is used to take all the items in an array and combine them into a single value (like
total, sum, or object).
Syntax:
return updatedAccumulator;
}, initialValue);
}, 0);
console.log(total); // 60
👉 Final answer = 60
const users = [
];
acc[user.id] = user.name;
return acc;
}, {});
console.log(userMap);
// { 1: "Arun", 2: "Bala" }
find() is used to get the first element in the array that satisfies a condition.
Syntax:
const students = [
];
console.log(topper);
// ✅ find
const val1 = arr.find(num => num > 25); // 30
// ✅ filter
const val2 = arr.filter(num => num > 25); // [30, 40]