Web Analytics

Advanced Array Methods

Modern ~20 min read

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.
HTML
CSS
JS

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.
HTML
CSS
JS

Flattening Arrays

The flat() method creates a new array with all sub-array elements concatenated into it recursively up to the specified depth.

HTML
CSS
JS

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.

HTML
CSS
JS

Summary

  • Use find() to get a single element based on a condition.
  • Use some() and every() for boolean checks.
  • Use flat() to flatten nested arrays.

Quick Quiz

Which method checks if ALL elements in an array pass a test?

A
some()
B
checkAll()
C
every()
D
all()