Search⌘ K
AI Features

Object Iteration

Discover key JavaScript iteration methods such as find, every, and some for arrays, and learn how to loop through object properties using for-in loops and Object methods. Understand destructuring to simplify access to key-value pairs, enabling effective data manipulation in your code.

The find() method

The find() method works in a similar way to the filter() method, but it returns the first value that matches the criteria defined in the callback. For example, the following code returns the first number that’s greater than 22:

Javascript (babel-node)
console.log([1, 2, 3, 4].find(x => x > 2));

The following code finds the first programming language that begins with the letter “J”:

Javascript (babel-node)
console.log(['C', 'C++', 'Ruby', 'Python', 'JavaScript', 'Swift', 'Java'].find(word => word.startsWith('J')));

We can use this to find people in our people array from the Guess Who? game that match the given criteria. The following example finds the first person who wears glasses, but not a hat:

Javascript (babel-node)
console.log(people.find(person => person.glasses && !person.hat).name);

Notice that, because the ...