Arrays & Objects
Array Methods
These methods replace most loops in modern JavaScript. They don't change the original array; they return a new value.
| Method | Returns |
|---|---|
map(fn) | A new array with each item transformed |
filter(fn) | A new array with only items that pass a test |
find(fn) | The first item that passes, or undefined |
some(fn) / every(fn) | true if any / all items pass |
reduce(fn, start) | One value built from all items |
forEach(fn) | Nothing; just runs fn for each item |
sort(fn) | Sorts in place (changes the array) |
const students = [
{ name: "Ali", marks: 85 },
{ name: "Sara", marks: 92 },
{ name: "Bilal", marks: 48 },
];
const names = students.map((s) => s.name);
const passed = students.filter((s) => s.marks >= 50);
const top = students.find((s) => s.marks > 90);
const total = students.reduce((sum, s) => sum + s.marks, 0);
console.log(names); // ["Ali", "Sara", "Bilal"]
console.log(passed.length); // 2
console.log(top.name); // "Sara"
console.log(total / students.length); // 75