single([predicate, arg])

Returns the only element of a sequence that satisfies a specified condition, and throws an exception if more than one such element exists.

Syntax

Parameters

predicate
Type: function(value, key, arg): boolean
A function to test an element for a condition.
arg
An external argument.
Return Value

The single element of the input sequence that satisfies a condition.

Examples

var fruits2 = [ "orange", "apple" ];
var fruit2 = $from(fruits2).single();

if (!fruit2) {
    document.writeln("The collection does not contain exactly one element.");
}

/*
 This code produces the following output:

 The collection does not contain exactly one element.
*/

var fruits = [ "apple", "banana", "mango", "orange", "passionfruit", "grape" ];
var fruit1 = $from(fruits).single("$length > 10");

document.writeln(fruit1);

/*
 This code produces the following output:

 passionfruit
*/			

Last edited May 18, 2012 at 9:13 AM by suckgamoni, version 1

Comments

No comments yet.