all(predicate[, arg])

suckgamoni edited this page May 14, 2013 · 2 revisions

Determines whether all elements of a sequence satisfy a condition.

Syntax

Parameters

predicate

Type: function(value, key, arg)
A function to test each element for a condition.

arg

An extra argument.

Return Value

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


Example

// Create an array of Pets.
var pets = [ { name: "Barley", age: 10 },
			 { name: "Boots", age: 4 },
			 { name: "Whiskers", age: 6 } ];

// Determine whether all pet names 
// in the array start with 'B'.
var allStartWithB = from(pets).all("$name.charAt(0) == 'B'");

console.log((allStartWithB ? "All" : "Not all") + " pet names start with 'B'.");

// This code produces the following output:
//
//  Not all pet names start with 'B'. 
var people = [
    { lastName: "Haas",
      pets: [ { name: "Barley", age: 10 },
              { name: "Boots", age: 14 },
              { name: "Whiskers", age: 6 } ] },
    { lastName: "Fakhouri",
	  pets: [ { name: "Snowball", age: 1 } ] },
    { lastName: "Antebi",
	  pets: [ { name: "Belle", age: 8 } ] },
    { lastName: "Philips",
	  pets: [ { name: "Sweetie", age: 2 },
	          { name: "Rover", age: 13 } ] }
];

// Determine which people have pets that are all older than 5.
var names = from(people)
    .where("from($pets).all('$age > 5')")
    .select("$lastName")
    .each("console.log($)");

/* This code produces the following output:
 * 
 * Haas
 * Antebi
 */