|Written by Ian Elliot
|Thursday, 05 January 2017
Page 2 of 3
The Access Methods
For example the pop and push methods can be used to remove an item from the end of the array or add an item:
adds A to the end of the array and increases length by 1.
Similar to the pop and push methods, the Shift and Unshift methods remove and add items to the start of the array. These two operations are ideal for implementing a queue or deque.
The Array Manipulation Methods
The concat method returns a new array that is the concatenation of two other arrays:
stores a new array in C that is the result of putting the Arrays A and B together (with B's elements following A's). You can concat more than two Arrays using A.concat(B,C) etc.
The slice method extracts a sub array from an existing array:
creates a new array in B that starts at element A and ends at element A. That is slice(s,e) returns the array from element s to element e but not including element e.
Similarly the splice method can be used to remove or insert elements:
will create a new array in B with five elements missing started at element 3, You can also specify new values to insert:
deletes two elements starting at element 3 and then inserts three elements starting at element 3.
Given these methods handle an array as if it was a type of string, there is also the join method which converts an Array into a string with an optional separator. There is also the very useful string function, split , which will split a string into an Array based on a specified separator character.
Array Ordering Methods
As one of the prime properties of an array is that it stores its elements in a fixed order, there should be methods that change this order.
The reverse method will reverse an array in place, i.e. it actually modifies the array:
reverses the order of the elements in A.
Similarly sort will put the elements into order:
sorts the elements of A into numerical order.
If the elements of A are not numeric values, then you have to supply an order function that compares its two parameters a and b and returns -1 if a<b, 1 if a>b and 0 if a=b.
For example to sort strings on string length alone you might use:
For example, you can store an object literal, or any object in an array element:
In this case myArray is an object. This means you can write things like
which evaluates to 1 the value of property1 on the object literal.
Sometimes you need to pass a variable number of complicated things to a function. One way of doing this in a very flexible way is to use an array parameter:
The receiving function can simply process its single parameter as an array with, in this case, the first element an object and the second two numeric values.
Function arrays are useful when you are trying to implement something that responds to an event or condition or when you need to select which function is used by an index.
Now to call the first function you would use
or to call the ith function
For example, a 2 x 2 array can be defined as:
The first element of A , i.e. A, is ["a","b"] which is usually treated as the first row. To pick out the first element of the row we simply index again ["a","b"] which is "a". The double indexing can be done in one go as in:
The i, j element of the array is:
as you can in many languages.
You can generalize this idea to arrays of any size and dimensionality, but in practice you rarely need more than two dimensions.
In general using a multidimensional array is just as easy as a standard array, but there is one small problem. Normally if you try to use an Array element that hasn't been initialized you simply get an undefined value. In the case of a two-dimensional array, say, the situation is a little worse.
Consider what happens in this case:
As there is no A undefined is returned and undefined doesn't make any sense - the result is a runtime error.
Even more of a problem is that assignment doesn't work either.
If you try
again you will generate a runtime error rather than creating the element. The problem is that the Array isn't initialized to hold suitable row Array objects.
The solution is to initialize the rows of the array as follows:
The variable r sets the number of rows that you want and in this example the array is created with four rows - each an empty array. Now when we try and access A there is an empty array acting as the row and so  makes sense and we get undefined rather than an error. If you try to assign to A then this works because again A is an empty array  and assigning to  creates the element.
You can package the code snippet into a function to set up an array with any number of rows. You can extend the same idea to initialize arrays of arrays of arrays.
|Last Updated ( Thursday, 05 January 2017 )