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

Description:

As the name may suggest, this method retrieves items from the start of a sequence based on a passed predicate results. This predicate gets evaluated for each item in the collection until the first time that predicate returns false for one item. The following sample code retrieves items from the start of the array until a string with a length of ten or more characters is encountered.Even though further items exist in the array that pass the condition, these are not returned.

Samples:

let fruits = ['Apple', 'Banana', 'Cherry', 'Damson', 'Elderberry', 'Grape', 'Kiwi', 'Lemon', 'Melon', 'Orange'];

console.log('TakeWhile method continues retrieving items unti string length be bigger or equal than 10 ...');
let takeWhilePartitioned = fruits.asEnumerable().takeWhile(f => f.length < 10).toArray();
for (let index = 0; index < takeWhilePartitioned.length; index++)
  console.log(takeWhilePartitioned[index]);
Clone this wiki locally