Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

single([predicate, arg])

suckgamoni edited this page · 1 revision

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.


Example

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

if (!fruit2) {
    console.log("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");

console.log(fruit1);

/*
 This code produces the following output:

 passionfruit
*/          
Something went wrong with that request. Please try again.