The ArrayExtensions are included in the framework and offer some extensions to the Array object in javascript. The ArrayExtensions are inspired by Linq.
In all recent browsers, the arrow function can be used instead of the inline function declaration.
{ Init: function() { var items = [{ Id: 1, Name: "Number one" }, { Id: 3, Name: "Number three" }, { Id: 2, Name: "Number two" }]; items = items.OrderBy(function(a){ return a.Id; }); // OR items = items.OrderBy(a => a.Id); for (var i = 0; i < items.length; i++) { this.Add({ innerHTML: items[i].Name }); } } }
Filter array items
{ Init: function() { var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }]; items = items.Where(a => a.Id >= 2); for (var i = 0; i < items.length; i++) { this.Add({ innerHTML: items[i].Name }); } } }
{ Init: function() { var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }]; items = items.Skip(2).Take(1); for (var i = 0; i < items.length; i++) { this.Add({ innerHTML: items[i].Name }); } } }
Transform existing array items into new items
{ Init: function() { var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }]; items = items.Select(a => a.Id + 10); for (var i = 0; i < items.length; i++) { this.Add({ innerHTML: items[i] }); } } }
{ Init: function() { var items = [{ Id: 1, Name: "A" }, { Id: 2, Name: "B" }, { Id: 3, Name: "C" }]; this.Add({ innerHTML: "Min: "+ items.Min(function(a) { return a.Id }) }); this.Add({ innerHTML: "Max: "+ items.Max(function(a) { return a.Id }) }); this.Add({ innerHTML: "Average: "+ items.Average(function(a) { return a.Id }) }); } }
{ Init: function() { var items = [{ Name: "Double" }, { Name: "Double" }, { Name: "Single" }]; items = items.Unique(function(a) { return a.Name; }); for (var i = 0; i < items.length; i++) { this.Add({ innerHTML: items[i].Name }); } } }
{ Init: function() { var items = ["A","B","C","D","E","F","G","H","I","J"]; items = items.Randomize(); for (var i = 0; i < items.length; i++) { this.Add({ _: "span", innerHTML: items[i] }); } } }