🔧
Array & Object Methods

Other Array Methods

find, some, every, sort, includes
💡 find() ek detective hai (pehla matching suspect dhundta hai). some()/every() poll-takers hain ("kya KOI ek match karta hai?" vs "kya SAB match karte hain?"). sort() ek librarian hai jo books ko order mein arrange karta hai.

find() pehla matching ELEMENT return karta hai (undefined agar na mile). findIndex() pehla matching INDEX return karta hai. some() true/false deta hai — "koi bhi element condition match karta hai?". every() true/false deta hai — "SAB elements condition match karte hain?". includes() simple value-presence check.

const users = [
  { id: 1, name: "Aarav", active: true },
  { id: 2, name: "Riya", active: false }
];

const user = users.find(u => u.id === 2);
console.log(user);   // { id: 2, name: "Riya", active: false }

console.log(users.some(u => u.active));    // true — koi ek active hai
console.log(users.every(u => u.active));   // false — sab active nahi

const numbers = [3, 1, 4, 1, 5];
console.log(numbers.includes(4));   // true

numbers.sort((a, b) => a - b);   // ascending sort — MUTATES original!
console.log(numbers);              // [1, 1, 3, 4, 5]
🔧
find() ek detective hai (pehla matching suspect dhundta hai). some()/every() poll-takers hain ("kya KOI ek match karta hai?" vs "kya SAB match karte hain?"). sort() ek librarian hai jo books ko order mein arrange karta hai.
1 / 2
⚡ Quick Recap
  • find/findIndex = pehla match (element/index), undefined/-1 agar na mile
  • some = "koi ek?", every = "sab?"
  • sort() bina compare function ke = string-sort, numbers ke liye bug!
Is page mein (2 subtopics)

flat(depth) nested arrays ko "flatten" karta hai (ek level ki nesting hataता hai, ya depth specify karo). flatMap() map() + flat(1) combined hai — ek common combo, ek hi method mein.

const nested = [1, [2, 3], [4, [5, 6]]];

console.log(nested.flat());       // [1, 2, 3, 4, [5, 6]] — 1 level flatten
console.log(nested.flat(2));      // [1, 2, 3, 4, 5, 6] — 2 levels

const sentences = ["Hello World", "Foo Bar"];
console.log(sentences.flatMap(s => s.split(" ")));
// ["Hello", "World", "Foo", "Bar"] — map + flatten ek saath
💡Tip: flatMap() especially useful hai jab map() ka result khud arrays ho (jaise split() se) aur tumhe ek SINGLE flat array chahiye, array-of-arrays nahi.

Array.from() array-like objects (jaise NodeList, arguments) ya iterables ko ASLI arrays mein convert karta hai — taaki array methods (map/filter) use kar sako, jo array-like objects par directly available nahi hote.

const nodeList = document.querySelectorAll(".item");   // NodeList, array nahi
// nodeList.map(...)   // ERROR: NodeList mein map() nahi hai

const realArray = Array.from(nodeList);
realArray.map(el => el.textContent);   // ab kaam karta hai!

// Range generate karna — common trick:
const range = Array.from({ length: 5 }, (_, i) => i);
console.log(range);   // [0, 1, 2, 3, 4]
💡Tip: Array.from({length: n}, (_, i) => ...) ek popular trick hai numbers ka range generate karne ke liye — Python ke range() jaisa kaam karta hai, thoda verbose lekin native.