Home » Tech Tutorials » Web Development » JavaScript » JavaScript find(), some(), and every()

JavaScript find(), some(), and every()

JavaScript find(), some(), every()

Arrays లోని values ను search చేయడానికి మరియు conditions ను test చేయడానికి find(), some(), every() methods ఉపయోగిస్తాము.

find()

find() method condition ను true చేసే మొదటి element ను return చేస్తుంది. ఏ element కూడా condition ను satisfy చేయకపోతే undefined return అవుతుంది.

Syntax: array.find(callback)

some()

some() method array లో కనీసం ఒక element అయినా condition ను satisfy చేస్తుందా అని check చేస్తుంది. కనీసం ఒకటి true అయితే true, లేకపోతే false return చేస్తుంది.

Syntax: array.some(callback)

every()

every() method array లోని ప్రతి element condition ను satisfy చేస్తుందా అని check చేస్తుంది. అన్ని elements true అయితే true, ఒక్క element అయినా false అయితే false return చేస్తుంది.

Syntax: array.every(callback)

Callback function

ఈ మూడు methods లో callback function ప్రతి element పై run అవుతుంది. Callback కు సాధారణంగా element, index మరియు complete array values అందుబాటులో ఉంటాయి. కానీ beginners గా ముందుగా element value తో practice చేయడం సరిపోతుంది.

మూడు methods మధ్య తేడా

  • find() ఒక matching element ను return చేస్తుంది.
  • some() కనీసం ఒక element match అవుతుందా అని boolean result ఇస్తుంది.
  • every() అన్ని elements match అవుతున్నాయా అని boolean result ఇస్తుంది.

ఈ methods original array ను మార్చవు. అందువల్ల ఇవి non-mutating array methods గా పరిగణించబడతాయి.

Simple Explanation

ఒక class లో students marks ఉన్నాయని అనుకుందాం. find() తో 80 కంటే ఎక్కువ marks ఉన్న మొదటి student లేదా mark ను కనుగొనవచ్చు. some() తో కనీసం ఒక student pass అయ్యాడా అని తెలుసుకోవచ్చు. every() తో class లో ప్రతి student pass అయ్యాడా అని check చేయవచ్చు.

గుర్తుంచుకోండి: find() element ను ఇస్తుంది; some() మరియు every() true లేదా false ను ఇస్తాయి.

Real-Life Example

ఒక online shopping website లో products array ఉందని అనుకుందాం. ఒక నిర్దిష్ట product ను వెతికేందుకు find() ఉపయోగించవచ్చు. కనీసం ఒక product stock లో ఉందా అని తెలుసుకోవడానికి some() ఉపయోగించవచ్చు. అన్ని products కు price అందుబాటులో ఉందా అని verify చేయడానికి every() ఉపయోగించవచ్చు.

Code Examples

find() తో మొదటి matching value

const prices = [120, 450, 80, 300];
const firstExpensivePrice = prices.find(price => price > 200);

console.log(firstExpensivePrice);

find() array లో values ను మొదటి నుంచి check చేస్తుంది. 200 కంటే ఎక్కువ ఉన్న మొదటి price 450 కాబట్టి అదే return అవుతుంది. తరువాత ఉన్న 300 ను check చేసినా, అది మొదటి matching value కాదు.

450

some() తో కనీసం ఒక value ను check చేయడం

const marks = [35, 42, 28, 19];
const hasPassedStudent = marks.some(mark => mark >= 35);

console.log(hasPassedStudent);

some() కనీసం ఒక mark 35 లేదా అంతకంటే ఎక్కువ ఉందా అని check చేస్తుంది. 35 మరియు 42 condition ను satisfy చేస్తున్నాయి కాబట్టి result true.

true

every() తో అన్ని values ను check చేయడం

const ages = [18, 21, 25, 30];
const allAdults = ages.every(age => age >= 18);

console.log(allAdults);

every() ప్రతి age 18 లేదా అంతకంటే ఎక్కువ ఉందా అని check చేస్తుంది. అన్ని values condition ను satisfy చేస్తున్నాయి కాబట్టి result true.

true

Objects array లో find() ఉపయోగించడం

const users = [
  { name: "Ravi", role: "student" },
  { name: "Anu", role: "teacher" },
  { name: "Kiran", role: "student" }
];

const teacher = users.find(user => user.role === "teacher");
console.log(teacher.name);

Objects array లో ప్రతి object యొక్క role property ను check చేస్తున్నాము. teacher role ఉన్న మొదటి object ను find() return చేస్తుంది. ఆ object నుండి name property ను print చేస్తున్నాము.

Anu

Line-by-Line Explanation

const prices = [120, 450, 80, 300];

ఈ line లో నాలుగు numeric values ఉన్న prices అనే array ను create చేస్తున్నాము.

const firstExpensivePrice = prices.find(price => price > 200);

find() ప్రతి price ను check చేస్తుంది. price > 200 condition true అయ్యే మొదటి value ను firstExpensivePrice లో store చేస్తుంది.

console.log(firstExpensivePrice);

మొదటిసారి condition ను satisfy చేసిన price ను console లో print చేస్తుంది. ఆ value 450.

Try It

Try this JavaScript code:

Example

const marks = [72, 45, 88, 39, 91];

const firstHighMark = marks.find(mark => mark >= 80);
const hasLowMark = marks.some(mark => mark < 40);
const allPassed = marks.every(mark => mark >= 35);

console.log(firstHighMark);
console.log(hasLowMark);
console.log(allPassed);

Try It Yourself »

Practice

Practice:

ages array లో 18 లేదా అంతకంటే ఎక్కువ ఉన్న మొదటి age ను find() తో కనుగొనండి. కనీసం ఒక adult ఉన్నారా అని some() తో check చేయండి. ప్రతి age 13 లేదా అంతకంటే ఎక్కువ ఉందా అని every() తో check చేయండి.

Hint:

find() మరియు some() కోసం age >= 18 condition ఉపయోగించండి. every() కోసం age >= 13 condition ఉపయోగించండి.

Common Mistakes

find() result ఎప్పుడూ array అవుతుందని అనుకోవడం.

find() matching element ను మాత్రమే return చేస్తుంది. Match దొరకకపోతే undefined return అవుతుంది.

some() మరియు every() రెండూ ఒకే విధంగా పనిచేస్తాయని అనుకోవడం.

some() కు కనీసం ఒక match చాలు. every() లో మాత్రం అన్ని elements condition ను satisfy చేయాలి.

Callback లో condition బదులుగా value ను direct గా return చేయడం.

Callback లో సరైన boolean condition ఇవ్వండి. ఉదాహరణకు mark >= 35 వంటి expression ఉపయోగించండి.

find() original array ను మార్చుతుందని అనుకోవడం.

find(), some(), every() methods original array ను modify చేయవు.

Interview Questions

JavaScript లో find() method ఏమి return చేస్తుంది?

Answer:

Condition ను satisfy చేసే మొదటి element ను return చేస్తుంది. ఏ match లేకపోతే undefined return చేస్తుంది.

some() మరియు every() మధ్య తేడా ఏమిటి?

Answer:

some() కనీసం ఒక element condition ను satisfy చేస్తే true ఇస్తుంది. every() అన్ని elements condition ను satisfy చేసినప్పుడే true ఇస్తుంది.

find() method object arrays పై పనిచేస్తుందా?

Answer:

అవును. Object యొక్క property ను callback లో check చేసి matching object ను కనుగొనవచ్చు.

Exam Questions

const numbers = [10, 25, 30]; లో 20 కంటే ఎక్కువ ఉన్న మొదటి value ను కనుగొనడానికి ఏ method మరియు condition ఉపయోగిస్తారు?

Answer:

numbers.find(number => number > 20) ఉపయోగిస్తే result 25 వస్తుంది.

const marks = [40, 55, 70]; లో ప్రతి mark 35 లేదా అంతకంటే ఎక్కువ ఉందా అని check చేయడానికి ఏ method ఉపయోగించాలి?

Answer:

marks.every(mark => mark >= 35) ఉపయోగించాలి. Result true.

const values = [2, 4, 7]; లో కనీసం ఒక odd number ఉందా అని check చేయడానికి code రాయండి.

Answer:

values.some(value => value % 2 !== 0) ఉపయోగించాలి. Result true.

Quick Revision

  • find() మొదటి matching element ను return చేస్తుంది.
  • find() కు match లేకపోతే undefined వస్తుంది.
  • some() కనీసం ఒక element condition ను satisfy చేస్తే true ఇస్తుంది.
  • every() అన్ని elements condition ను satisfy చేస్తే మాత్రమే true ఇస్తుంది.
  • ఈ మూడు methods callback function ను ఉపయోగిస్తాయి.
  • ఈ methods original array ను మార్చవు.
  • Object arrays లో properties ను check చేయడానికి కూడా ఈ methods ఉపయోగించవచ్చు.

Test Your Knowledge

Take a short quiz based on this lesson.

10 Questions • Results
Scroll to Top