Skip to content
Eugene Sadovoi edited this page Jul 15, 2016 · 5 revisions

Determines whether all elements of a sequence satisfy a condition.

Syntax

All([predicate])

Parameters

predicate

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

Return Value

true if every element of the source sequence passes the test in the specified predicate, or if the sequence is empty; otherwise, false.

Remarks

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

Example

The following code example demonstrates how to use All to determine whether all the elements in a sequence satisfy a condition. Variable allStartWithB is true if all the pet names start with "B" or if the pets array is empty.

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

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

// Determine whether all pet names in the array start with 'B'.
var allStartWithB = Enumerable.asEnumerable(pets)
                              .All(pet => pet.Name.startsWith("B"));

//This code produces the following output:
// Not all pet names start with 'B'.
console.log(allStartWithB ? "All" : "Not all" + " pet names start with 'B'.");

Try it out

Clone this wiki locally