Lambda operations for js inspired by C# :)
npm install devbox-linq
require('devbox-linq');
only works on v6 and above
- add
- addRange
- all
- any
- count
- distinct
- distinctRecursive
- first
- firstOrDefault
- groupBy
- last
- lastOrDefault
- max
- min
- average
- order
- orderBy
- orderByDesc
- orderDesc
- remove
- removeAt
- select
- selectMany
- skip
- sum
- take
- thenBy
- thenByDesc
- where
Add a item into a existing array.
let array = [{ id: 1, name: 'Goku' }];
array.add({ id: 2, name: 'Vegeta' }); // Same as .push(), it's only semanthic
console.log(array);
/*
OUTPUT:
[
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
]
*/
Add a list of items into a existing array.
//Way 1
let array1 = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
let array2 = [
{ id: 3, name: 'Bulma' },
{ id: 4, name: 'Gohan' }
];
array1.addRange(array2);
console.log(array1);
/*
OUTPUT:
[
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' },
{ id: 3, name: 'Bulma' },
{ id: 4, name: 'Gohan' }
]
*/
//Way 2
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
array.addRange({ id: 3, name: 'Bulma' }, { id: 4, name: 'Gohan' });
console.log(array);
/*
OUTPUT:
[
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' },
{ id: 3, name: 'Bulma' },
{ id: 4, name: 'Gohan' }
]
*/
Checks that all items in the list match the condition.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
array.all(x => x.id == 1); // false
array.all(x => x.id >= 1); // true
Checks that one item in the list match the condition. If no condition, check if has something in the list.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
array.any(x => x.id == 3); // false
array.any(x => x.id == 1); // true
array.any(); // true
Count all items that match the condition. If no condition, returns the list length.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
array.count(x => x.id >= 2); //1
array.count(); //2
Distinguishes equal items from the list. If it's complex type, like object and array, it'll be by reference.
let array = [
{ text: 'Object' },
{ text: 'Object' }
];
array.distinct(); // Nothing happens
let obj = { text: 'Object2' };
array.add(obj); // Adding one time
array.add(obj); // Adding two times
array.distinct();
console.log(array);
/*
OUTPUT:
[
{ text: 'Object' },
{ text: 'Object' },
{ text: 'Object2' }
]
*/
[1, 2, 3, 4, 1, 2].distinct(); // [1, 2, 3, 4]
Distinguishes equal items from the list. If it's complex type, like object, it'll be done verifying the attributes.
let array = [
{ text: 'Object' },
{ text: 'Object' }
];
array.distinctRecursive(); // [{ text: 'Object' }]
let obj = { text: 'Object2' };
array.add(obj); // Adding one time
array.add(obj); // Adding two times
array.distinctRecursive();
console.log(array);
/*
OUTPUT:
[
{ text: 'Object' },
{ text: 'Object2' }
]
*/
Returns the first item that match the condition.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' },
{ id: 2, name: 'Vegeta2' }
];
array.first(x => x.id == 2); // { id: 2, name: 'Vegeta' }
array.first(); // { id: 1, name: 'Goku' }
array.first(x => x.id == 3); // throws exception
[].first(); // throws exception
Returns the first item that match the condition, if anyone match, returns undefined instead exception.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' },
{ id: 2, name: 'Vegeta2' }
];
array.firstOrDefault(x => x.id == 2); // { id: 2, name: 'Vegeta' }
array.firstOrDefault(); // { id: 1, name: 'Goku' }
array.firstOrDefault(x => x.id == 10); // undefined
[].firstOrDefault(); // undefined
Group the list by a expression.
let array = [
{ id: 1, name: 'Goku', age: 20 },
{ id: 2, name: 'Vegeta', age: 20 },
{ id: 3, name: 'Bulma', age: 19 }
];
let group = array.groupBy(x => x.age);
console.log(group);
/*
OUTPUT:
[
[
{ id: 1, name: 'Goku', age: 20 },
{ id: 2, name: 'Vegeta', age: 20 }
],
[
{ id: 3, name: 'Bulma', age: 19 }
]
]
*/
console.log(group[0].key); // 20
console.log(group[1].key); // 19
Returns the last item that match the condition.
let array = [
{ id: 1, name: 'Goku' },
{ id: 1, name: 'Goku2' },
{ id: 2, name: 'Vegeta' }
];
array.last(x => x.id == 1); // { id: 1, name: 'Goku2' }
array.last(); // { id: 2, name: 'Vegeta' }
array.last(x => x.id == 3); // throws exception
[].last(); // throws exception
Returns the last item that match the condition, if anyone match, returns undefined instead exception.
let array = [
{ id: 1, name: 'Goku' },
{ id: 1, name: 'Goku2' },
{ id: 2, name: 'Vegeta' }
];
array.lastOrDefault(x => x.id == 1); // { id: 1, name: 'Goku2' }
array.lastOrDefault(); // { id: 2, name: 'Vegeta' }
array.lastOrDefault(x => x.id == 10); // undefined
[].lastOrDefault(); // undefined
Returns the largest item that match the expression.
let array = [
{ id: 1, name: 'Goku', age: 22 },
{ id: 2, name: 'Vegeta', age: 20 }
];
array.max(x => x.age); // 22
[1, 5, 3].max(); // 5
Returns the smallest item that match the expression.
let array = [
{ id: 1, name: 'Goku', age: 22 },
{ id: 2, name: 'Vegeta', age: 20 }
];
array.min(x => x.age); // 20
[1, 5, 3].min(); // 1
Returns the average of list that match the expression.
[10, 20, 30].average(); // 20
Order list ascending.
[2, 3, 5, 1, 4].order(); // [1, 2, 3, 4, 5]
['B', 'C', 'E', 'A', 'D'].order(); // ['A', 'B', 'C', 'D', 'E']
Order list ascending by expression.
let array = [
{ id: 1, name: 'Goku', age: 22 },
{ id: 2, name: 'Vegeta', age: 20 },
{ id: 3, name: 'Bulma', age: 15 }
];
array.orderBy(x => x.age);
console.log(array);
/*
OUTPUT:
[
{ id: 3, name: 'Bulma', age: 15 },
{ id: 2, name: 'Vegeta', age: 20 },
{ id: 1, name: 'Goku', age: 22 }
]
*/
array.orderBy(x => x.name);
console.log(array);
/*
OUTPUT:
[
{ id: 3, name: 'Bulma', age: 15 },
{ id: 1, name: 'Goku', age: 22 },
{ id: 2, name: 'Vegeta', age: 20 }
]
*/
[5, 4, 3, 2, 1].orderBy(x => x); // [1, 2, 3, 4, 5] - Same as .order()
Order list descending by expression.
let array = [
{ id: 1, name: 'Goku', age: 15 },
{ id: 2, name: 'Vegeta', age: 20 },
{ id: 3, name: 'Bulma', age: 22 }
];
array.orderByDesc(x => x.age);
console.log(array);
/*
OUTPUT:
[
{ id: 3, name: 'Bulma', age: 22 },
{ id: 2, name: 'Vegeta', age: 20 },
{ id: 1, name: 'Goku', age: 15 }
]
*/
array.orderByDesc(x => x.name);
console.log(array);
/*
OUTPUT:
[
{ id: 2, name: 'Vegeta', age: 20 },
{ id: 1, name: 'Goku', age: 15 },
{ id: 3, name: 'Bulma', age: 22 }
]
*/
[1, 2, 3, 4, 5].orderByDesc(x => x); // [5, 4, 3, 2, 1] - Same as .orderDesc()
Order list descending.
[2, 3, 5, 1, 4].orderDesc(); // [5, 4, 3, 2, 1]
['B', 'C', 'E', 'A', 'D'].orderDesc(); // ['E', 'D', 'C', 'B', 'A']
Remove items from the list.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
//By Condition
array.remove(x => x.id == 2);
console.log(array); //OUTPUT: [{ id: 1, name: 'Goku' }]
//By Ref
let goku = array.first(x => x.id == 1);
array.remove(goku);
console.log(array); //OUTPUT: []
Remove a item from the list by index.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
array.removeAt(1);
console.log(array); //OUTPUT: [{ id: 1, name: 'Goku' }]
Transform your list.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' }
];
array.select(x => x.id); // [1, 2]
array.select(x => x.id + 1); // [2, 3]
array.select(x => x.name + ' is very strong'); // ['Goku is very strong', 'Vegeta is very strong']
array.select(x => x = { user: x.id + ' - ' + x.name });
/*
OUTPUT:
[
{ user: '1 - Goku' },
{ user: '2 - Vegeta' }
]
*/
Joins multiple lists inside a object in one.
let array = [
{ id: 1, name: 'Goku', friends: ['Chi-Chi', 'Kuririn', 'Trunks', 'Gohan'] },
{ id: 2, name: 'Vegeta', friends: ['Bulma', 'Trunks'] }
];
let allFriends = array.selectMany(x => x.friends);
console.log(allFriends); // ['Chi-Chi', 'Kuririn', 'Trunks', 'Gohan', 'Bulma', 'Trunks']
console.log(allFriends.distinct()); // ['Chi-Chi', 'Kuririn', 'Gohan', 'Bulma', 'Trunks']
Skip the length informed.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' },
{ id: 3, name: 'Bulma' }
];
let arraySkipped = array.skip(2);
console.log(arraySkipped); // [{ id: 3, name: 'Bulma' }]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].skip(5); // [6, 7, 8, 9, 10]
Sum the items by expression.
let array = [
{ id: 1, name: 'Goku', power: 8001 },
{ id: 2, name: 'Vegeta', power: 7000 }
];
array.sum(x => x.power); // 15001
[1, 2, 3, 4, 5].sum(); // 15
Take the length informed.
let array = [
{ id: 1, name: 'Goku' },
{ id: 2, name: 'Vegeta' },
{ id: 3, name: 'Bulma' }
];
let arrayTook = array.take(1);
console.log(arrayTook); // [{ id: 1, name: 'Goku' }]
arrayTook = array.skip(1).take(1);
console.log(arrayTook); // [{ id: 2, name: 'Vegeta' }]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].take(5); // [1, 2, 3, 4, 5]
[1, 2, 3, 4, 5, 6, 7, 8, 9, 10].skip(3).take(3); // [4, 5, 6]
Order the list ascending by a second expression after some "order" method (order, orderBy, orderByDesc or orderDesc)
let array = [
{ id: 1, name: 'Goku', age: 20 },
{ id: 2, name: 'Vegeta', age: 22 },
{ id: 3, name: 'Bulma', age: 20 }
];
array.orderBy(x => x.age).thenBy(x => x.name);
console.log(array);
/*
OUTPUT
[
{ id: 3, name: 'Bulma', age: 20 },
{ id: 1, name: 'Goku', age: 20 },
{ id: 2, name: 'Vegeta', age: 22 }
]
*/
//Order the even first, then put them in the ascending order
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1].orderBy(x => x % 2 == 0 ? 0 : 1).thenBy(x => x);
//OUTPUT: [2, 4, 6, 8, 10, 1, 3, 5, 7, 9]
Order the list descending by a second expression after some "order" method (order, orderBy, orderByDesc or orderDesc)
let array = [
{ id: 1, name: 'Goku', age: 20 },
{ id: 2, name: 'Vegeta', age: 22 },
{ id: 3, name: 'Bulma', age: 20 }
];
array.orderBy(x => x.age).thenByDesc(x => x.name);
console.log(array);
/*
OUTPUT
[
{ id: 1, name: 'Goku', age: 20 }
{ id: 3, name: 'Bulma', age: 20 }
{ id: 2, name: 'Vegeta', age: 22 }
]
*/
//Order the even first, then put them in the descending order
[10, 9, 8, 7, 6, 5, 4, 3, 2, 1].orderBy(x => x % 2 == 0 ? 0 : 1).thenByDesc(x => x);
//OUTPUT: [10, 8, 6, 4, 2, 9, 7, 5, 3, 1]
Filter items that matches the condition
let array = [
{ id: 1, name: 'Goku', power: 8001 },
{ id: 2, name: 'Vegeta', power: 7000 },
{ id: 3, name: 'Bulma', power: 12 },
];
let strongers = array.where(x => x.power > 5000);
console.log(strongers);
/*
OUTPUT:
[
{ id: 1, name: 'Goku', power: 8001 },
{ id: 2, name: 'Vegeta', power: 7000 }
]
*/
[1, 2, 3, 4, 5, 6].where(x => x % 2 == 0); // [2, 4, 6]