liveBook · Manning

Any

As explained in Secrets of the JavaScript Ninja, Third Edition

A JavaScript method that returns true if at least one array element satisfies a predicate.

Need to know whether at least one item meets a condition? Call `some` on an array with a callback that tests each item. It returns a boolean: `true` if at least one item satisfies the condition, otherwise `false`. section 10.1.4 section 10.5

Check whether anyone has a weapon

`const ninjas = [{ name: "Yagyu", weapon: "shuriken" }, { name: "Yoshi" }];` `const someoneIsArmed = ninjas.some(ninja => "weapon" in ninja);` `console.assert(someoneIsArmed);` The callback checks for a `weapon` property. One matching object is enough for the result to be `true`. section 10.1.4

Choose `every` when all items must pass the test. Choose `find` when you need a matching item, or `filter` when you need the matching items. Use `some` when the answer you need is simply whether any item qualifies. section 10.1.4

How Secrets of the JavaScript Ninja, Third Edition covers it

How deep it goes
Demonstrates a property-check callback alongside `every`, without detailing edge cases or callback invocation rules.
Examples
Checks whether any object in an array has a `weapon` property.
What it assumes
Uses arrays of objects, property checks, and arrow-function callbacks without defining those constructs.
What you can do after
Apply it via JavaScript's `some` method to answer whether any array item meets a specified condition.
How it connects
Contrasts `some` with `every`, `find`, and `filter` as ways to test or retrieve matching array items.

Also explained in

Compare how 3 books explain any →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Secrets of the JavaScript Ninja, Third Edition. The section links point to the parts of the book it draws on.