Advanced Array Methods
Searching Arrays
Beyond indexOf, modern JavaScript offers powerful search
methods.
find(): Returns the value of the first element that passes a test.findIndex(): Returns the index of the first element that passes a test.
Testing Arrays
You can check if elements in an array pass a test.
some(): Checks if at least one element passes a test.every(): Checks if all elements pass a test.
Flattening Arrays
The flat() method creates a new array with all
sub-array elements concatenated into it recursively up to the
specified depth.
FlatMap
The flatMap() method first maps each element using
a mapping function, then flattens the result into a new array.
It is identical to a map followed by a flat of depth 1.
Summary
- Use
find()to get a single element based on a condition. - Use
some()andevery()for boolean checks. - Use
flat()to flatten nested arrays.
Quick Quiz
Which method checks if ALL elements in an array pass a test?
Enjoying these tutorials?