defaultIfEmpty(defValue)

Returns the elements of the specified sequence or the specified value in a singleton collection if the sequence is empty.

Syntax

Parameters

defValue
The value to return if the sequence is empty.
Return Value

An iterable object that contains defValue if source is empty; otherwise, source.

Examples

var defaultPet = { name: "Default Pet", age: 0 };

var pets1 = [ { name: "Barley", age: 8 },
              { name: "Boots", age: 4 },
              { name: "Whiskers", age: 1 } ];

document.writeln("pets1");

$from(pets1).defaultIfEmpty(defaultPet).each(function(pet) {
    document.writeln("Name: " + pet.name);
});

var pets2 = [];

document.writeln("pets2");

$from(pets2).defaultIfEmpty(defaultPet).each(function(pet) {
    document.writeln("Name: " + pet.name);
});

/*
 This code produces the following output:

 pets1
 Name: Barley
 Name: Boots
 Name: Whiskers

 pets2
 Name: Default Pet
*/

Last edited May 9, 2013 at 9:30 AM by suckgamoni, version 2

Comments

No comments yet.