Linq methods for JavaScript/TypeScript for working with arrays
This simple extension works with array of complex objects as well as simple arrays of strings etc. The whole thing is written in TypeScript but also usable in JavaScript
This extension is lightweight and fast and you can use your Lambda-Expression-Syntax to work with arrays. The methods are mostly identically to .NET methods.
As expressions you can use the normal Function-Syntax:
array.Where(function(x){returnx.Name=="Max";});Lambda-Expressions (IE isn't compatible with this Lambda-Expressions):
array.Where(x=>x.Name=="Max");or Lambda-Expressions as Strings (this Syntax works in IE):
array.Where("x => x.Name == 'Max'");Also a complete procedure as an sql-like string is supported
array.Evaluate("select x => x.Id sum");
clone
reverse
where x => x.Age > 70
order by x => x.Name
then by x => x.FirstName descending
for each x => console.log(x)
select x => {x.Name}
- Works with multiple Browsers (even IE)
- Angular Support (event directly in Views if using Strings as Expression-Syntax)
- Lightweight
- Fast
- Syntax from .NET
- Build on top of array-prototype and no changes in code are required for usage
- Integrates seamlessly into the project
- TypeScript definitions
npm install linq4js
bower install linq4js
Include this line in your project
<scripttype="text/javascript" src="linq4js.js"></script>Use
import "linq4js";
or
<scripttype="text/javascript" src="linq4js.js"></script>to import the scripts and optionally (if you are not using npm) install @types/linq4js to also get tooling support.
Creates a copy of the array
vararray=["item1","item2","item3","item4","no"];//["item1", "item2", "item3", "item4", "no"]array.Clone();Gets the index of the first item found by a filter
vararray=["item1","item2","item3","item4","no"];//2array.FindIndex("x => x == 'item3'");Gets the index of the last item found by a filter
vararray=["item1","item2","item3","item2","item4","no"];//3array.FindIndex("x => x == 'item2'");Gets the item with the index
vararray=["item1","item2","item3","item4","no"];//"item3"array.Get(2);Repeats an object in the array
vararray=["item1","item2","item3","item4"];//["item1", "item2", "item3", "item4", "example", "example", "example"]array.Repeat("example",3);Executes a method for each item in the array
vararray=["item1","item2","item3","item4","no"];array.ForEach("i => console.log(i)");Updates object(s) in the array
By default this method uses the property Id to identify the objects. If the property is not set this methods tries to compare the objects directly.
vararray=[{Id: 1,Value: "item1"},{Id: 2,Value: "item2"}];//[{Id: 1, Value: "item3"}, {Id: 2, Value: "item2"}]array.Update({Id: 1,Value: "item3"});If you want this method to use other fields for identification define a selector function as second parameter.
vararray=[{OtherId: 1,Value: "item1"},{OtherId: 2,Value: "item2"}];//[{Id: 1, Value: "item3"}, {Id: 2, Value: "item2"}]array.Update({OtherId: 1,Value: "item3"},"x => x.OtherId");You can upgrade multiple objects simultaneously
vararray=[{OtherId: 1,Value: "item1"},{OtherId: 2,Value: "item2"}];//[{Id: 1, Value: "item3"}, {Id: 2, Value: "item4"}]array.UpdateRange([{OtherId: 1,Value: "item3"},{OtherId: 2,Value: "item4"}],"x => x.OtherId");Removes item(s) from array
By default this method uses the property Id to identify the objects. If the property is not set this methods tries to compare the objects directly.
vararray=["item1","item2","item3","item4","no"];//["item1", "item2", "item3", "item4"]array.Remove("no");//["item1", "item2"]array.RemoveRange(["item4","item3"]);If you want this method to use other fields for identification define a selector function as second parameter.
vararray=[{OtherId: 1,Value: "item1"},{OtherId: 2,Value: "item2"}];//[{OtherId: 2, Value: "item2"}]array.Remove({OtherId: 1},"x => x.OtherId");Adds the item(s) to the array
vararray=["item1","item2","item3","item4","no"];//["item1", "item2", "item3", "item4", "no", "item5"]array.Add("item5");//["item1", "item2", "item3", "item4", "no", "item5", "item6", "item7"]array.AddRange(["item6","item7"]);Inserts an entry at a specific position
vararray=["item1","item2","item3","item4"];//["item1", "item2", "item2.5", "item3", "item4"]array.Insert("item2.5",2);Searches for all items in array that match the given filter
vararray=["item1","item2","item3","item4","no"];//["item1", "item2", "item3", "item4"]array.Where("i => i.match(/item/gi)");Takes items in a specific range
vararray=["item1","item2","item3","item4"];//["item2", "item3"]array.Range(1,2);Returns the length of the array or if a filter is set the length of the resulting array
vararray=["item1","item2","item3","item4","no"];//5array.Count();//4array.Count("i => i.match(/item/gi)");Compares to sequences of objects
vararray=["item1","item2","item3"];vararray2=["item1","item2","item3"];vararray3=["item","item2","item3"];//truearray.SequenceEqual(array2);//falsearray.SequenceEqual(array3);Tests if any item is in the array and if a filter is set if any item of the array matches the filter
vararray=["item1","item2","item3","item4","no"];//truearray.Any();//truearray.Any("i => i.length > 2");//falsearray.Any("i => i == ''");Tests if all items in the array match the condition
vararray=["item1","item2","item3","item4","no"];//falsearray.All("i => i.length > 2");Tests if array contains specific object
vararray=["item1","item2","item3","item4","no"];//falsearray.Contains("test");Combines two arrays
vararray=["item1","item2","item3"];vararray2=["item4","no"];//["item1", "item2", "item3", "item4", "no"]array.Concat(array2);Combines two arrays but only applies values that are in both arrays
vararray=["item1","item2","item3"];vararray2=["item1","unique","item2","item3"];//["item1", "item2", "item3"]array.Intersect(array2);Combines two arrays without duplicates
vararray=["item1","item2","item3"];vararray2=["item1","unique","item2","item3"];//["item1", "item2", "item3", "unique"]array.Union(array2);Joins the entries by the given char
vararray=["item1","item2","item3","item4","no"];//item1-item2-item3-item4-noarray.Join("-");//item1-item2-item3-item4array.Join("-","x => x.length > 2");Combines the entries using a custom function
vararray=["item1","item2","item3","item4","no"];//no-item4-item3-item2-item1array.Aggregate("(str, item) => item + '-' + item");Converts the array to a dictionary
vararray=[{OtherId: 1,Value: "item1"},{OtherId: 2,Value: "item2"}];//{1: {OtherId: 1, Value: "item1"}, 2: {OtherId: 2, Value: "item2"}}array.ToDictionary("x => x.OtherId");//{1: "item1", 2: "item2"}array.ToDictionary("x => x.OtherId","x => x.Value");Combines the entries of two arrays using a custom function
vararray=[0,1,2,3,4];vararray2=["zero","one","two","three"];//["0 zero", "1 one", "2 two", "3 three"]array.Zip(array2,"(x, y) => x + ' ' + y");Reverses the array
vararray=["item1","item2","item3","item4","no"];//["no", "item4", "item3", "item2", "item1"]array.Reverse();Computes the average of the elements
vararray=[{val: 5},{val: 3},{val: 1}];//3array.Average("x => x.val");//4array.Average("x => x.val","x => x.val > 1");vararray2=[3,4,5];//4array2.Average();Computes the sum of the elements
vararray=[{val: 5},{val: 3},{val: 1}];//9array.Sum("x => x.val");//8array.Sum("x => x.val","x => x.val > 1");vararray2=[3,4,5];//12array2.Sum();Returns the first item of the array and if a filter was set the first item that matches the filter - Throws an exception if no item was found
vararray=["no","item1","item2","item3","item4","no"];//"no"array.First();//"item1"array.First("i => i.match(/item/gi)");//Exceptionarray.First("i => i == 'notgiven'");Returns the first item of the array and if a filter was set the first item that matches the filter - returns null if no suitable item was found
vararray=["no","item1","item2","item3","item4","no"];//"no"array.FirstOrDefault();//"item1"array.FirstOrDefault("i => i.match(/item/gi)");//nullarray.First("i => i == 'notgiven'");Returns the only item of the array - Throws an exception if not exactly one item is in array
vararray=["item1"];//"item1"array.Single();vararray=["item1","item2"];//"item1"array.Single("x => x == 'item1'");//Exceptionarray.Single();Returns the only item of the array - Throws an exception if not only one item is in array
vararray=["item1"];//"item1"array.Single();vararray=["item1","item2"];//"item1"array.Single("x => x == 'item1'");//Exceptionarray.Single();//nullarray.Single("x => x == 'item3'");Returns the smallest element in array
vararray=[0,8,1,5,-3];//-3array.Min();vararray=[{name: "test",age: 3},{name: "test2",age: 18}];//{name: "test", age: 3}array.Min("x => x.age");Returns the last item of the array and if a filter was set the last item that matches the filter - Throws an exception if no item was found
vararray=["no","item1","item2","item3","item4","no"];//"no"array.Last();//"item4"array.Last("i => i.match(/item/gi)");//Exceptionarray.Last("i => i == 'notgiven'");Returns the last item of the array and if a filter was set the last item that matches the filter - returns null if no suitable item was found
vararray=["no","item1","item2","item3","item4","no"];//"no"array.LastOrDefault();//"item4"array.LastOrDefault("i => i.match(/item/gi)");//nullarray.LastOrDefault("i => i == 'notgiven'");Returns the greates element in array
vararray=[0,8,1,5,-3];//8array.Max();vararray=[{name: "test",age: 3},{name: "test2",age: 18}];//{name: "test2", age: 18}array.Max("x => x.age");Select the properties for a new array
vararray=[{Id: 1,Value: "item1"},{Id: 2,Value: "item2"}];//["item1", "item2"]array.Select("i => i.Value");//[{Custom: 1, Name: "item1"}, {Custom: 2, Name: "item2"}];array.Select("i => {Custom: i.Id, Name: i.Value}");When using the string syntax it is also possible to assign objects by the following methods
vararray=[{Id: 1,Value: "item1"},{Id: 2,Value: "item2"}];//[{Value: item1}, {Value: item2}]array.Select("i => {i.Value}");//[{C: item1}, {C: item2}]array.Select("i => {C: i.Value}");//[{C: item1}, {C: item2}]array.Select("i => {C = i.Value}");Select the properties with an array as value and concats them
vararray=[["item1","item2"],["item1","item2"]];//["item1", "item2", "item1", "item2"]array.SelectMany("i => i");Limits the number of entries taken
vararray=["item1","item2","item3","item4"];//["item1", "item2"]array.Take(2);Takes entries as long as a condition is true
vararray=["item1","item2","item3","item2","item4"];varitem2count=0;//["item1", "item2", "item3"]array.TakeWhile(function(x){if(x=="item2"){item2count++;}returnitem2count<2;});This is the basic usage. But if you want conditional executes for e.g. with counting this can get a little bit messy.
vararray=["item1","item2","item3","item2","item4"];//["item1", "item2", "item3"]array.TakeWhile(function(item,storage){returnitem!="item2"||storage.count<1;//Condition},function(storage){storage.count=0;//Init the Storage},function(item,storage){if(item=="item2"){storage.count++;//After executing the condition}});Skips entries
vararray=["item1","item2","item3","item4"];//["item3", "item4"]array.Skip(2);Groups array by property
vararray=[{name: "Max",age: 17},{name: "Emily",age: 54},{name: "max",age: 32},{name: "emily",age: 12}];//[// [{name: "Emily", age: 54},{name: "emily", age: 12}],// [{name: "Max", age: 17},{name: "max", age: 32}]//]array.GroupBy("i => i.name.toLowerCase()");Orders array by property or value
vararray=["item3","item1","item2","item4"];//["item4", "item3", "item2", "item1"]array.OrderByDescending("i => i");//["item1", "item2", "item3", "item4"]array.OrderBy("i => i");Also supports complex properties
vararray=[{Name: "Max",Lastname: "Mustermann"},{Name: "John",Lastname: "Doe"},{Name: "Erika",Lastname: "Mustermann"}];//[{Name: "Max", Lastname: "Mustermann"},//{Name: "Erika", Lastname: "Mustermann"},//{Name: "John", Lastname: "Doe"}]array.OrderByDescending("i => i.Lastname");//[{Name: "John", Lastname: "Doe"}, //{Name: "Max", Lastname: "Mustermann"},//{Name: "Erika", Lastname: "Mustermann"}]array.OrderBy("i => i.Lastname");Orders array by additional properties in combination with OrderBy/OrderByDescending
vararray=[{Name: "Max",Lastname: "Mustermann"},{Name: "John",Lastname: "Doe"},{Name: "Erika",Lastname: "Mustermann"}];//[{Name: "Erika", Lastname: "Mustermann"},//{Name: "Max", Lastname: "Mustermann"},//{Name: "John", Lastname: "Doe"}]array.OrderByDescending("i => i.Lastname").ThenBy("i => i.Name");Moves an item from one index to another
vararray=["item1","item2","item3","item4"];//["item1", "item3", "item4", "item2"]array.Move(1,3);Makes all values unique using the specified selector
vararray=["item1","item2","item2","item3","item4"];//["item1", "item2", "item3", "item4"]array.Distinct("x => x");array.Distinct();Evaluates SQL-String for array
vararray=[...];array.Evaluate("...");Example SQL-string
clone
reverse
where x => x.Age > 70
order by x => x.Name
then by x => x.FirstName descending
for each x => console.log(x)
select x => {x.Name}
Supported methods (the methodname and aliases are not case-sensitive)
| Methodname | Alias | Examples |
|---|---|---|
| Clone | clone | |
| Reverse | reverse | |
| Contains | contains 5 | |
| Join | join 5 | |
| Sum | sum | |
| Average | average | |
| Where | where x => x.Id > 3 | |
| Select | select x => x.Id | |
| SelectMany | select many select ... many | select many x => x.Pets |
| Get | get 4 | |
| ForEach | for each | for each x => console.log(x) |
| Count | count x => x.Id | |
| All | all x => x.Age > 4 | |
| Any | any x => x.Age > 4 | |
| Take | take 3 | |
| TakeWhile | take while take ... while | take while x => x.Age > 10 |
| Skip | skip 3 | |
| Min | min x => x.Age | |
| Max | max x => x.Age | |
| GroupBy | group by | group by x => x.Name |
| Distinct | distinct x => x.Id | |
| FindLastIndex | find last index find index ... last findindex ... last | find index x => x.Age == 3 last |
| FindIndex | find index find first index findfirstindex find index ... first findindex ... first | find index x => x.Age == 3 first |
| OrderByDescending | order by ... descending orderby ... descending orderby descending order by descending orderbydescending | order by x => x.Age descending |
| OrderBy | order by ... ascending orderby ... ascending orderby ascending order by ascending orderbyascending order by orderby | order by x => x.Age ascending |
| FirstOrDefault | first or default | first or default |
| LastOrDefault | last or default | last or default |
| SingleOrDefault | single or default | single or default |
| First | first | |
| Last | last | |
| Single | single | |
| ThenByDescending | thenby ... descending then by ... descending thenbydescending then by descending | then by x => x.Name descending |
| ThenBy | thenby ... ascending then by ... ascending thenbyascending then byascending thenby then by | then by x => x.Name ascending |