These tutorials are about JavaScript and its use for ViSTA programming.
Since arrays and object are in many ways interchangeable in JavaScript, it would follows that arrays have methods, just as objects do. We discuss methods in detail when we talk about objects, but for now let us look at the basics of array methods.
Almost all of the methods of arrays are non-static.
A static method is one that can be used like a function,
independent of any specific array or object. Conversely, non-static arrays must
occur as properties of specific instances of an array. For instance, assume for
the following that arr01 and arr02 are both arrays:
// This is not a valid command vName = concat(arr01, arr02); // This is a valid command and concatenates arr02 to arr01 arr01.concat(arr02);
More detail on this available in the objects tutorials.
join and reverseThese methods convert the elements of an array to strings and then joins the string together into one long string. It returns this string for assignment. It does not change the contents of the array.
join joins the elements in order, while reverse joins them in reverse order.
Unless told otherwise, they will separate the elements in the string with commas. You can override this by specifying a string as an argument for the method. This string is used to separate the elements.
// this will leave s with the value of '1,2,3'
var aName = new Array [1, 2, 3];
s = aName.join();
// this will leave s with the value of '1 -- 2 -- 3'
s = aName.join(' -- ');
// this will leave s with the value of '3, 2' 1'
s - aName.reverse(', ');
sortThis method sorts the array into alphabetical order, or, more specifically, ASCII value order. In other words, the sort is case sensitive.
If you want to sort in a different order, you pass a function as an argument for the method. The function should compare two values and return a value that determines the order to sort them in. This value should be a negative number if the first item comes first, a positive number if the second item should come first, and zero if the two items are equal and can occur in either order. This method changes the contents of the array.
// to sort in ASCII order aName.sort( ); // to sort by function aName.sort(someFuncName(a, b));
concatThis function concatenates another array to the current array. The new array is appended at the end.
Elements that are themselves arrays are linearized and included as a series of elements. This linearizing only occurs to one level of nesting. If that doesn't make sense, then just avoid concatenating nested arrays.
// this code will concatenate arr02 to arr01 // arr01 will then equal (1, 2, 3, 4, 5, 6) var arr01 = new Array(1, 2, 3); var arr02 = new Array(4, 5, 6); arr01.concat(arr02); // this code will concatenate arr02 to arr01 // arr01 will then equal (1, 2, 3, 4, 5, 6, 7) // note that arr02[2] equals the array containing // the values (6, 7) var arr01 = new Array(1, 2, 3); var arr02 = new Array(4, 5, [6, 7]); arr01.concat(arr02);
slice and spliceThese methods work with sub-arrays. slice slices
out a section of the array.
It takes two arguments, the starting position and the position after the
ending position. If either number is negative it is counted from the end of the
string. slice does not change the array. It returns a
value that needs to be assigned to some variable.
var aName = new Array(1, 2, 3, 4, 5, 6, 7, 8, 9); a = aName.slice(0,4); // returns (1, 2, 3) a = aName.slice(5); // returns (6, 7, 8, 9) a = name.slice(2, -2); // returns (3, 4, 5, 6, 7)
splice inserts one array in another in a specified
location, as well as deleting elements from an array. It can both add and
delete.
It takes a number of arguments.
This method changes the array. Anything cut out of the array is returned from the method and can be passed to a variable.
// this would set a to (4, 5, 6) // and aName to (1, 2, 3) var aName = new Array(1, 2, 3, 4, 5, 6); a = aName.splice(2); // this would set aName to (1, 2, 5, 6) // and a to (3, 4) var aName = new Array(1, 2, 3, 4, 5, 6); a = aName.splice(2, 2); // this would set aName to (1, 2, 'c', 'd', 5, 6) // and a (3, 4) var aName = new Array(1, 2, 3, 4, 5, 6); a = aName.splice(2, 2, 'c', 'd');
The splice method is new and is not as well
supported as the slice method. Here is the same
process performed with slice and with splice. Full support of splice
will make code just a little easier to read.
// aName set to (1, 2, 'c', 'd', 5, 6) with slice var aName = new Array(1, 2, 3, 4, 5, 6); var newName = Array( ); b = aName.slice(4); a = aName.slice(0, 2); newName.concat(a, 'c', 'd', c); // aName set to (1, 2, 'c', 'd', 5, 6) with splice var aName = new Array(1, 2, 3, 4, 5, 6); aName.splice(2, 2, 'c', 'd');