ప్రధాన కంటెంట్‌కు వెళ్లండి
eLearner.app
మాడ్యూల్ 6 · 4లో పాఠం 3కోర్సులో 23/32~10 min
మాడ్యూల్ పాఠాలు (3/4)

కనుగొనండి, కొన్ని, ప్రతి

Three compact methods to search in an array without writing the loop by hand.

find: the first one that matches

JS
const utenti = [
  { nome: 'Anna', eta: 30 },
  { nome: 'Luca', eta: 12 },
  { nome: 'Sara', eta: 18 },
];

utenti.find((u) => u.eta >= 18);
// { nome: 'Anna', eta: 30 }

utenti.find((u) => u.nome === 'Marco');
// undefined  ← if none matches

findIndex variant: returns the index (or -1).

some: at least one

JS
const nums = [1, 2, 3, 4, 5];

nums.some((n) => n > 4); // true
nums.some((n) => n > 100); // false

[].some(() => true); // false  ← on an empty array it is always false

every: all of them

JS
const nums = [1, 2, 3, 4, 5];

nums.every((n) => n > 0); // true
nums.every((n) => n > 2); // false

[].every(() => false); // true  ← on an empty array it is always true!

The asymmetry of some and every on empty arrays is intentional and logically consistent with the "exists" (∃) and "for all" (∀) logical operators.

Try it

వ్యాయామం#js.m6.l3.e1
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Define `firstAdult(users)`: given an array of { name, age }, return the name of the first one with age >= 18, or null if none. Use find.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

users.find(...); if found return u.name, otherwise null.

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది

Review exercise

వ్యాయామం#js.m6.l3.e2
ప్రయత్నాలు: 0లోడ్ అవుతోంది...

Define `allPositive(nums)`: return true if all elements of nums are > 0 (and the array is NOT empty), otherwise false. Use every + a length check.

ఎడిటర్ లోడ్ అవుతోంది…
సూచనను చూపించు

length > 0 && nums.every(n => n > 0)

3 ప్రయత్నాల తర్వాత పరిష్కారం లభిస్తుంది