Array Extensions

NinJa extends the base Array object found in JavaScript and adds a lot of the same functionality we've all grown to love about the List object in .NET. As always, there is full Intellisense for arrays. The old functions remain, but new functions have been added along with wrapper functions with .Net naming (like .Add instead of .push for adding items).

Adding and Removing Items

Adding and removing items is pretty easy. Use the following functions to manipulate an array:

That's a lot of functions for just manipulate arrays. There is one other way to create an array:

Speaking of iterations, there are two functions added that allows you to execute the same code on each item in an array:

When using iteration functions, the this keyword will refer to the calling object, that is, the array itself. 

Iterating Arrays

Using the ForEach and ForEachReversed functions can be pretty handy, so here's a few things you need to know.

First, the function operates on a copy of the array. So if you modify the array in anyway, the iteration won't be affected as it's using the state of the array at the time the iteration function started.

Second, the first parameter passed to the function is the item at the current index. The second parameter is of the type IterationEventArgs. This is a special class containing information about the current iteration. There are advanced fields in this that can be used to traverse the array.

Iteration Control & IterationEventArgs

IterationEventArgs contains the following fields:

Here's an example code where we remove all the strings in an array.

$Table("datagrid").Rows.ForEach(function(i, e){
    i.BackgroundColor(Colors.LightBlue);
    e.Navigate = 1;
});

This code snippet will give every other row in the table a light blue background color. It's yet another useless example that perfectly illustrates the power of navigating the ForEach and ForEachReversed iteration functions.

Searching Arrays

Three functions are provided for Arrays that allow you to search them for specific objects: