JavaScript AcademyProfessional Engineering

Learning object 19 of 100

Array search explorer

Run every search method on the same array: indexOf, includes, find, findIndex, some, every and filter, side by side.

Loading…

What it shows

indexOf and includes compare each item with the value using ===. find, findIndex, some, every and filter call your test function for each item instead, which is how you search objects by a property. find returns the first matching item, filter returns all of them, and some and every answer yes-or-no questions.

Guided experiment

  1. Search for "Grace" in the lower-case names. Why is it not found?
  2. In the product list, change the test to find the product named Lamp.
  3. Find a test where some is true but every is false.

Predict

What does [3, 8, 12].find((n) => n > 5) return?

Related

Lessons: Lesson 28: Searching and sorting arrays, Lesson 29: Iterating over collections

Projects: Project 9: Shopping Cart Price Calculator

Source: web/assets/js/objects/object-019.js in the companion repository.