-
Notifications
You must be signed in to change notification settings - Fork 26
Any
Eugene Sadovoi edited this page Jul 12, 2016
·
8 revisions
Determines whether any element of a sequence exists or satisfies a condition.
Any([predicate])
A function to test each element for a condition. Boolean predicate(TSource)
true
if any elements in the source sequence pass the test in the specified predicate; otherwise, false
.
The enumeration of source collection is stopped as soon as the result can be determined.
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.");