Skip to content
Yasser Moradi edited this page Aug 26, 2015 · 2 revisions

Description:

This method returns the first item from sequence. We can use this method without passing any argument.

Sample:

let items = ['One', 'Two', 'Three', 'Four', 'Five', 'Six', 'Seven', 'Eight', 'Nine', 'Ten'];

let first = items.asEnumerable().first();

console.log('*** return first element of the array');

console.log(first);

Description:

If there are no items in the in the sequence the method throws an exception.

let empty = [];
try {
  first = empty.asEnumerable().first();
}
catch (err) {
  console.error(err.message);
}

Description:

You can pass an argument as a predicate. The returned value is the first item of the list that matches that predicate criteria.

Sample:

let first = items.asEnumerable().first(item => item.length == 5);

console.log('first item is equals 5 length => ' + first);
Clone this wiki locally