Skip to content
Eugene Sadovoi edited this page Jul 12, 2016 · 8 revisions

Determines whether any element of a sequence exists or satisfies a condition.

Syntax

Any([predicate])

Parameters

predicate

A function to test each element for a condition. Boolean predicate(TSource)

Return Value

true if any elements in the source sequence pass the test in the specified predicate; otherwise, false.

Remarks

The enumeration of source collection is stopped as soon as the result can be determined.

Example

The following code example demonstrates how to use Any to determine whether any element in a sequence satisfies a condition.

class Pet {
    constructor(name, age, vaccinated) {
        this.Name = name;
        this.Age = age;
        this.Vaccinated = vaccinated;
    }
}

// Create an array of Pets.
var pets = [ new Pet("Barley", 10, true),
             new Pet("Boots", 4, false),
             new Pet("Whiskers", 6, false) ];

// Determine whether any pets over age 1 are also unvaccinated.
var unvaccinated = Enumerable.asEnumerable(pets)
                             .Any(p => p.Age > 1 && p.Vaccinated == false);

// This code produces the following output:
// There are unvaccinated animals over age one.
console.log("There" + (unvaccinated ? "are" : "are not any") +
            "unvaccinated animals over age one.");

Try it out

Clone this wiki locally