count([predicate, arg])

suckgamoni edited this page May 14, 2013 · 2 revisions

Returns a number that represents how many elements in the specified sequence satisfy a condition.

Syntax

Parameters

predicate

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

arg

An external argument.

Return Value

Type: number A number that represents how many elements in the sequence satisfy the condition in the predicate function.


Example

var fruits = [ "apple", "banana", "mango", "orange", "passionfruit", "grape" ];

var numberOfFruits = from(fruits).count();
console.log("There are " + numberOfFruits + " fruits in the collection.");

// This code produces the following output:
//
// There are 6 fruits in the collection. 
var pets = [ { name: "Barley", vaccinated: true },
             { name: "Boots", vaccinated: false },
             { name: "Whiskers", vaccinated: false } ];

var numberUnvaccinated = from(pets).count("!$vaccinated");
console.log("There are " + numberUnvaccinated + " unvaccinated animals.");
					
// This code produces the following output:
//
// There are 2 unvaccinated animals.