Linq to Objects

Table Of Contents

the methods with the icon are native javascript methods. I indicate them in the documentation for more facilities (they are not rewritten)
native method in chrome

Array Methods

add

(this) add( (*) value)
    

(*) value : a value
    

the current array with the value
    

var a=[].add(5).add(6); // [5,6]

addRange

(this) addRange( Array values)
    

values: a array of value

the current array with elements of values
    

var a = [].addRange([5,6].addRange[7,8,9]); // [5,6,7,8,9]

clear

(this) clear()
    

Remove all elements of the current array

None
    

the current array cleared
    

var a=[1, 2, 3, 4, 5]; a.clear(); //=> a=[]

concat

Array concat( (...*) values)
    

Creates a new array concatenating array with any additional arrays and/or values.

[values] (...*): The values to concatenate.
    

(Array): Returns the new concatenated array.
    

var myArray = [1]; var other = myArray.concat(2, [3], [[4]]); // => others = [1, 2, 3, [4]] // => myArray = [1]

aggregate

aggregate(seed,accumulate,resultSelector)
    

An aggregation operation computes a single value from a collection of values. An example of an aggregation operation is calculating the average daily temperature from a month's worth of daily temperature values.

seed: The initial accumulator value.
    accumulate: An accumulator function to be invoked on each element.
    resultSelector: A function to transform the final accumulator value into the result value.
    

The transformed final accumulator value.
    

The following illustration shows the results of two different aggregation operations on a sequence of numbers. The first operation sums the numbers. The second operation returns the maximum value in the sequence.

Aggregate applies a method to each element. It applies a function to each successive element. With this extension method, we act upon the aggregate of all previous elements. This makes certain methods, such as sum, possible.

var fruits = ["apple", "mango", "orange", "passionfruit", "grape"]; var longestName = fruits.aggregate((longest, next) => next.length > longest.length ? next : longest, fruit => fruit.toUpperCase()); // => longestName = PASSIONFRUIT

all

all(predicate)
    

true if all the elements in a collection match a certain condition.

predicate: 
    

any

any(predicate)
    

true if almost one element in a collection match a certain condition.

predicate:
    

average

average(selector)
    

Provides a declarative way to compute the average value of a sequence

var grades = [78, 92, 100, 37, 81]; var average = grades.average(); // => average = 77.6

clear

(this) clear()
    

Remove all elements of the current array

None
    

the current array cleared
    

var a = [1,2,3,4,5]; a.clear(); // => a = []

count

count(predicate)
    

the number of elements in a sequence.

predicate:A sequence that contains elements to be counted.
    
    

var fruits = ["apple", "banana", "mango", "orange", "passionfruit", "grape"]; fruits.count(); // 6 fruits.count(x => x.includes("n")); // 4

distinct

distinct(predicate)
    

distinct elements from a sequence.

var ages = [21, 46, 46, 55.3, 17, 21, 55.4, 55.5, 55.4, 55.3]; ages.distinct(); // [ 21, 46, 55.3, 17, 55.4, 55.5 ] ages.distinct(Math.floor); // [ 21, 46, 55, 17 ]

equals

boolean equals(Array anotherArray)
    

anotherArray : a array
    

true if the array contains same value of anotherArray in same position, else false

var items = [ { name: "Edward", value: 21 }, { name: "Sharpe", value: 37 }, { name: "And", value: 45 }, { name: "The", value: -12 }, { name: "Magnetic", value: 13 }, { name: "Zeros", value: 37 } ]; var items2 = items.shuffle(); items.equals(items); // true items.equals(items2); //false [1,2,3,"a","b","c"].equals([1,2,3,"a","b","c"]); // true

filter

Array filter(predicate)

Iterates over elements of collection, returning an array of all elements predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

 predicate: The function invoked per iteration.
    

(Array): Returns the new filtered array.
    

var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false } ]; users.filter(o => !o.active); // { 'user': 'fred', 'age': 40, 'active': false }

find

Array find(predicate, number fromIndex=0)

Iterates over elements of collection, returning the first element predicate returns truthy for. The predicate is invoked with three arguments: (value, index|key, collection).

predicate: The function invoked per iteration.
    fromIndex: The index to search from.
    

(*): Returns the matched element, else undefined.
    

var users = [ { 'user': 'barney', 'age': 36, 'active': true }, { 'user': 'fred', 'age': 40, 'active': false }, { 'user': 'pebbles', 'age': 1, 'active': true } ]; users.find(o => o.age < 40); // { 'user': 'barney', 'age': 36, 'active': true }

findIndex

number findIndex(predicate)

This method is like find except that it returns the index of the first element predicate returns truthy for instead of the element itself.

predicate: The function invoked per iteration
    

(number): Returns the index of the found element, else -1.
    

var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true } ]; users.findIndex(o => o.user == 'barney'); // => 0

findLast

(*) findLast(predicate)

This method is like find except that it iterates over elements of collection from right to left.

predicate: The function invoked per iteration
    

(*): Returns the matched element, else null.
    

[1, 2, 3, 4].findLast(n=>n % 2 == 1); // => 3

findLastIndex

number findLastIndex(predicate)

This method is like findIndex except that it iterates over elements of collection from right to left.

predicate: The function invoked per iteration
    

(number): Returns the index of the found element, else -1.
    

var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': false } ]; users.findLastIndex(o==>o.user == 'pebbles'); // => 2

first

first(predicate)
    

(*): Returns the first element in a sequence that satisfies a specified condition.
    throws an exception if source contains no elements. To instead return a default value when the source sequence is empty, use the firstOrDefault method.
    

[1, 2, 3].first(); // => 1 try { [].first(); } catch(e) { console.log(e); } // =&gt; Exception

firstOrDefault

firstOrDefault(predicate,defaultValue)
    

the first element of a sequence that satisfies a specified condition, or a default value if no element is found.

predicate:
    defaultValue:
    

var names = ["Hartono Tommy", "Adams Terry","Andersen Henriette Thaulow","Hedlund Magnus", "Ito Shu"]; names.firstOrDefault(name =&gt; name.length &gt; 20)); // "Andersen Henriette Thaulow" names.firstOrDefault(name =&gt; name.length &gt; 200, "not found")); // "not found"

forEach

forEach(action)
    

Iterates over elements of collection and invokes iteratee for each element. The iteratee is invoked with three arguments: (value, index|key, collection). Iteratee functions may exit iteration early by explicitly returning false.

Note: As with other "Collections" methods, objects with a "length" property are iterated like arrays. To avoid this behavior use forIn or forOwn for object iteration.

action : The function invoked per iteration.
    

None

[1, 2].forEach(function(value) { console.log(value); }); // =&gt; Logs `1` then `2`.

getRange

Array getRange(number start, number end)
    

returns a new array whose elements are those of the original array between the start and end positions

start: start position in the array
    end: end position in the array
    

a new array whose elements are those of the original array between the start and end positions
    

var a=[0,1,2,3,4,5,6,7,8,9]; var b=a.getRange(5,8); console.log(a); // [0,1,2,3,4,5,6,7,8,9] console.log(b); // [5,6,7,8]

groupBy

Array groupBy(keySelector)
    

Groups the elements of a sequence.

Groups the elements of a sequence according to a specified key selector function and creates a result value from each group and its key. The elements of each group are projected by using a specified function.

keySelector : The iteratee to transform keys.
    

(Object): Returns the composed aggregate object.
    

[6.1, 4.2, 6.3].groupBy(Math.floor); /* [ { key: '4', elements: [ 4.2 ] }, { key: '6', elements: [ 6.1, 6.3 ] } ] */

includes

boolean includes( (*) value, number fromIndex=0)
    

Checks if value is in collection. If collection is a string, it's checked for a substring of value, otherwise SameValueZero is used for equality comparisons. If fromIndex is negative, it's used as the offset from the end of collection.

value: The value to search for.
    fromIndex: The index to search from.
    

(boolean): Returns true if value is found, else false.
    

[1, 2, 3].includes( 1); // =&gt; true [1, 2, 3].includes(1, 2); // =&gt; false

includesAll

boolean includesAll(Array array)
    

array : a array
    

true if the array contains all value of array, else false
    

[1, 2, 3, "a", "b", "c"].includesAll([1, 2, 3, "a", "b", "c"]); // true [1, 2, 3, "a", "b", "c"].includesAll([1, 2, 3, "d", "a", "b", "c"]); // false

indexOf

number indexOf(any value, number fromIndex=0)
    

Gets the index at which the first occurrence of value is found in array using SameValueZero for equality comparisons. If fromIndex is negative, it's used as the offset from the end of array.

value (*): The value to search for.
    fromIndex: The index to search from.
    

(number): Returns the index of the matched value, else -1.
    

[1, 2, 1, 2].indexOf(2); // =&gt; 1 [1, 2, 1, 2].indexOf( 2, 2); // =&gt; 3

innerJoin

(this) innerJoin(Array array,string condition,[string] fields)
    

    array : the array to inner join
        condition: a left field = a right field
        fieds : array of field in right array to extends left array
    

    (Array): return a new array with fields of right array and left array whose matches with condition
    

var items = [ { name: "Edward", value: 21 }, { name: "Sharpe", value: 37 }, { name: "Alfred", value: 45 }, { name: "Thomas", value: -12 }, { name: "Paul", value: 13 }, { name: "Tibo", value: 37 }, { name: "Manuel", value: 38 } ]; var jobTitle = [ { name: "Sharpe", job: "technician", exp: "senior" }, { name: "Edward", job: "ingenieer", exp: "junior" }, { name: "Manuel", job: "florist", exp: "junior" }, { name: "Paul", job: "electrician", exp: "senior" }, { name: "Laurent", job: "builder", exp: "junior" }, ]; arr = items.innerJoin(jobTitle, "name=name", ["job", "exp"]); /* [ { name: 'Edward', value: 21, job: 'ingenieer', exp: 'junior' }, { name: 'Sharpe', value: 37, job: 'technician', exp: 'senior' }, { name: 'Paul', value: 13, job: 'electrician', exp: 'senior' }, { name: 'Manuel', value: 38, job: 'florist', exp: 'junior' } ] */

insertAt

(this) insertAt(number position, any value)
    

Insert a value at the position of the current array. If position is greater than array length, then insert at last position.

position: position where insert the value in the array
    value: the value to insert
    

the current array with new values
    

var a=[7, 8, 9, 10]; a.insertAt(0, 11); // => a = [11, 7, 8, 9, 10] a.insertAt(50, 12).insertAt(4, 13); // => a = [11, 7, 8, 9, 13, 10, 12]

insertRangeAt

(this) insertRangeAt(number position, Array values)
    

Insert elements of values at the position of the current array. If position is greater than array length, then insert at last position.

position: position where insert the value in the array
    values: element of array to insert
    

the current array with new values
    

var a=[7, 8, 9, 10]; a.insertRangeAt(2,[12, 13, 14, 15, 16]); // => a = [7, 8, 12, 13, 14, 15, 16]

intersect

intersect(second,comparer)
    

Produces the set intersection of two sequences by using the specified comparer to compare values.

[1, 2, 3, 1, 2, 3, 4, 5, 1, 2, 8].intersect([9, 10, 1, 3]); // [1,3]

join

string join(string separator=',')
    
      

Converts all elements in array into a string separated by separator.

separator: The string separator.
    

(string): Returns the joined string.
    

['a', 'b', 'c'].join('~'); // =&gt; 'a~b~c'

last

any last(predicate)
    

Returns the last element from a collection, or the last element that satisfies a condition. last has two overload methods. One overload method doesn't take any input parameter and returns last element from the collection. Second overload method takes a lambda expression to specify a condition and returns last element that satisfies the specified condition. method returns the last element from a collection, or the last element that satisfies the specified condition.If a given collection is empty or does not include any element that satisfied the condition then it will throw InvalidOperation exception.

predicate:
    

(*): Returns the last element from a collection, or the last element that satisfies a condition.
    

[1, 2, 3, 4, 5, 2, 3, 8].last(x => x); // => 8 [1, 2, 3, 4, 5, 2, 3, 8].last(x => x &lt; 3)); // => 2 [1, 2, 3, 4, 5, 2, 3, 8].last(); // => 8

lastIndexOf

number lastIndexOf(value, number fromIndex=array.length-1)
    

This method is like indexOf except that it iterates over elements of array from right to left.

value: The value to search for.
    fromIndex: The index to search from.
    

(number): Returns the index of the matched value, else -1.
    

[1, 2, 1, 2].lastIndexOf(2); // => 3 // Search from the `fromIndex`. [1, 2, 1, 2].lastIndexOf(2, 2); // => 1

leftJoin

Array leftJoin(Array array,string condition,[string] fields)
    

array : the array to left join

    (Array): return the current array with fields of right array whose matches with condition
    

var items = [ { name: "Edward", value: 21 }, { name: "Sharpe", value: 37 }, { name: "Alfred", value: 45 }, { name: "Thomas", value: -12 }, { name: "Paul", value: 13 }, { name: "Tibo", value: 37 }, { name: "Manuel", value: 38 } ]; var jobTitle = [ { name: "Sharpe", job: "technician", exp: "senior" }, { name: "Edward", job: "ingenieer", exp: "junior" }, { name: "Manuel", job: "florist", exp: "junior" }, { name: "Paul", job: "electrician", exp: "senior" }, { name: "Laurent", job: "builder", exp: "junior" }, ]; var arr = items.leftJoin(jobTitle, "name=name", ["job"]); /*[ { name: 'Edward', value: 21, job: 'ingenieer' }, { name: 'Sharpe', value: 37, job: 'technician' }, { name: 'Alfred', value: 45 }, { name: 'Thomas', value: -12 }, { name: 'Paul', value: 13, job: 'electrician' }, { name: 'Tibo', value: 37 }, { name: 'Manuel', value: 38, job: 'florist' } ]*/

max

max(selector,defaultValue)
    

the maximum value in a sequence of values.

var numbers = [9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12,19]; numbers.max(number => number); // 435 numbers.max(); // 435 [].max(x=>x,-50); // -50

min

min(selector,defaultValue)
    

Returns the minimum value in a sequence of values.

var numbers = [9, 34, 65, 92, 87, 435, 3, 54, 83, 23, 87, 435, 67, 12,19]; numbers.min(number => number); // 3 numbers.min(); // 3 [].min(x=>x,-50); // -50

orderBy

Array orderBy(keySelector,comparer)
    

sort the array according to the field keySelector

keySelector:
    comparer:
    

(Array): Returns the new sorted array.
    

var users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 36 } ]; users.orderBy(x=>x.age); /* [ { 'user': 'barney', 'age': 34 }, { 'user': 'barney', 'age': 36 }, { 'user': 'fred', 'age': 40 }, { 'user': 'fred', 'age': 48 }, ] */

orderByDescending

Array orderByDescending(keySelector,comparer)
    

sort the array according to the field keySelector

keySelector:
    comparer:
    

(Array): Returns the new sorted array.
    

var users = [ { 'user': 'fred', 'age': 48 }, { 'user': 'barney', 'age': 34 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 36 } ]; users.orderByDescending(x=>x.age); /* [ { 'user': 'fred', 'age': 48 }, { 'user': 'fred', 'age': 40 }, { 'user': 'barney', 'age': 36 }, { 'user': 'barney', 'age': 34 }, ];*/

push

(this) push(value)
    
    push() adds one or more elements to the end of an array and returns the resulting length of the array.
    

value: a value
    

var myArray = new Array('1', '2'); myArray.push('3'); // myArray is now ["1", "2", "3"]

reduce

any reduce(Function iteratee=_.identity, any accumulator)
    

Reduces collection to a value which is the accumulated result of running each element in collection thru iteratee, where each successive invocation is supplied the return value of the previous. If accumulator is not given, the first element of collection is used as the initial value. The iteratee is invoked with four arguments: (accumulator, value, index|key, collection).

(*): Returns the accumulated value.
    

[1, 2].reduce(function(sum, n) { return sum + n; }, 0); // => 3

removeAll

number removeAll(predicate)
    

Removes all elements from array that predicate returns truthy for and returns an array of the removed elements. The predicate is invoked with three arguments: (value, index, array).

Note: Unlike filter, this method mutates array.

predicate: The function invoked per iteration.
    

Returns the number of removed elements.
    

var myArray = [1, 2, 3, 4,8,10,12,14]; var nb = myArray.removeAll(n=>n % 2 == 0); console.log(array); // => [1, 3] console.log(nb); // => 6

removeAt

(this) removeAt(index)
    

Removes a element from array at position index

Returns the array without removed element
    

var ints = [0,1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; ints.removeAt(5); // [ 0,1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12] ints ; // [ 0,1, 2, 3, 4, 6, 7, 8, 9, 10, 11, 12]

removeRange

(this) removeRange(index, count)
    

Remove elements from array between position index and index+count

the array without deleted elements

var ints = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]; ints.removeRange(5, 5); // [ 1, 2, 3, 4, 5, 11, 12] ints; // [ 1, 2, 3, 4, 5, 11, 12 ]

replace

(this) replace(any value,any newValue)
    
    Replace the element of the array whose match with value and replace by newValue
    

value: the value search for
    newValue: the value to replace with
    

the current array with replaced value
    

objs = [ { x: 1, y: 1 }, { x: 2, y: 2 }, { x: 3, y: 3 }, { x: 4, y: 4 }, { x: 1, y: 10 }, { x: 2, y: 20 }, { x: 3, y: 30 }, { x: 4, y: 40 }, { x: 1, y: 100 }, { x: 2, y: 200 }, { x: 3, y: 300 }, { x: 4, y: 400 } ]; objs.replace({ x: 1 }, { x: "one", y: 1, z: 1 }); /* [ { x: 'one', y: 1, z: 1 },{ x: 2, y: 2 },{ x: 3, y: 3 },{ x: 4, y: 4 }, { x: 'one', y: 1, z: 1 },{ x: 2, y: 20 },{ x: 3, y: 30 },{ x: 4, y: 40 }, { x: 'one', y: 1, z: 1 },{ x: 2, y: 200 },{ x: 3, y: 300 },{ x: 4, y: 400 } ] */

reverse

Array reverse(Array array)
    

Reverses array so that the first element becomes the last, the second element becomes the second to last, and so on.

Note: This method mutates array and is based on reverse.

array (Array): The array to modify.
    

(Array): Returns array.
    

var myArray = [1, 2, 3]; myArray.reverse(array); // => [3, 2, 1] console.log(array); // => [3, 2, 1]

sort

(this) sort(comparaison)
    

comparaison: a comparaison fonction
    

current array sorted
    

var items = [ { name: "Edward", value: 21 }, { name: "Sharpe", value: 37 }, { name: "And", value: 45 }, { name: "The", value: -12 }, { name: "Magnetic", value: 13 }, { name: "Zeros", value: 37 } ]; items.sort( (a, b) => a.value - b.value); // [ { name: 'The', value: -12 }, // { name: 'Magnetic', value: 13 }, // { name: 'Edward', value: 21 }, // { name: 'Sharpe', value: 37 }, // { name: 'Zeros', value: 37 }, // { name: 'And', value: 45 } ]

take

Array take(number n=1)
    

Creates a slice of array with n elements taken from the beginning.

n: The number of elements to take.
    

(Array): Returns the slice of array.
    

[1, 2, 3].take(); // => [1] [1, 2, 3].take(2); // => [1, 2] [1, 2, 3].take(5); // => [1, 2, 3] [1, 2, 3].take( 0); // => []

takeWhile

Array takeWhile(Function predicate=_.identity)
    

Creates a slice of array with elements taken from the beginning. Elements are taken until predicate returns falsey. The predicate is invoked with three arguments: (value, index, array).

predicate: The function invoked per iteration.
    

(Array): Returns the slice of array.
    

var users = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true }, { 'user': 'sabrina', 'active': false } ]; var users4 = users.takeWhile(o => !o.active); /* => user4 = [ { 'user': 'barney', 'active': false }, { 'user': 'fred', 'active': false } ] */

union

Array union(second, comparer)
    

Creates an array of unique values from all given arrays using comparer for equality comparisons.

second: The arrays to inspect
    comparer:the function comparer 2 items
    

(Array): Returns the new array of combined values.
    

[2, 2, 2, 3].union([1, 2]); // => [2, 3,1]

whileIndex

Number whileIndex(Function predicate)
    

predicate: The function invoked per iteration.
    

(Number): Returns the first index of element of array where predicate doesn't correspond.
    

var users = [ { 'user': 'barney', 'active': true }, { 'user': 'fred', 'active': false }, { 'user': 'pebbles', 'active': true }, { 'user': 'sabrina', 'active': false } ]; var pos = users.whileIndex(o => !o.active); // => 1

where

Array where(predicate)
    

predicate: The function invoked per iteration.
    

(Array): Returns a array with only elements corresponding to the predicate.

[2, 2, 2, 3, 2].where(x=>x===2); // => [2, 2, 2, 2]