Create an Array object // onevaraValuesnewArray (); // twovaraValuesnewArray (20); // threevaraColorsnewArray (); aColors [0] & quot; red & quot ;; aColors [1] & quot; green & quot; aColors [2] & quot
Create an Array object
// One
Var aValues = new Array ();
// Two
Var aValues = new Array (20 );
// Three
Var aColors = new Array ();
AColors [0] = "red ";
AColors [1] = "green ";
AColors [2] = "blue ";
// Four
Var aColors = new Array ("red", "green", "blue ");
// Five
Var aColors = ["red", "green", "blue"];
Join & split
Join: connection string
Var aColors = ["red", "green", "blue"];
Alert (aColors. join (","); // outputs "red, green, blue"
Alert (aColors. join ("-spring-"); // outputs "red-spring-green-spring-blue"
Alert (aColors. join ("] ["); // outputs "red] [green] [blue"
Split: split string
Var sColors = "red, green, blue ";
Var aColors = sColors. split (","); // outputs ["red", "green", "blue"]
Var redColors = aColors [0]. split (""); // outputs ["r", "e", "d"]
Concat & slice
Concat: append an array
Var aColors = ["red", "green", "blue"];
Var aColors2 = aColors. concat ("yellow", "purple ");
Alert (aColors); // outputs ["red", "green", "blue"]
Alert (aColors2); // outputs ["red", "green", "blue", "yellow", "purple"]
Slice: returns a new array with specific items
Var aColors = ["red", "green", "blue", "yellow", "purple"];
Var aColors2 = aColors. slice (1); // outputs ["green", "blue", "yellow", "purple"]
Var aColors3 = aColors. slice (1, 4); // outputs ["green", "blue", "yellow"]
Push & pop
Like the stack, Array provides the push and pop methods. The push method is used to add one or more items at the end of Array. pop is used to delete the last Array item and return it as the function value.
Var stack = new Array;
Stack. push ("red ");
Stack. push ("green ");
Stack. push ("blue ");
Alert (stack); // outputs ["red", "green", "blue"]
Var vItem = stack. pop ();
Alert (vItem); // outputs ["blue"]
Alert (stack); // otputs ["red", "green"]
Shift & unshift
Shift: Delete the first item in the array and use it as the function return value. unshift: place an item in the first position of the array, and then move the remaining items downward to www.2cto.com.
Var aColors = ["red", "green", "blue"];
Var vItem = aColors. shift ();
Alert (aColors); // outputs ["green", "blue"]
Alert (vItem); // outputs ["red"]
AColors. unshift ("black ");
Alert (aColors); // outputs ["black", "green", "blue"]
Reverse & sort
Reverse: reverse the order of array items. sort: sort the values of array items in ascending order (first, call the toString () method to convert all values into strings)
Var aColors = ["blue", "green", "red"];
AColors. reverse ();
Alert (aColors); // outputs ["red", "green", "blue"]
AColors. sort ();
Alert (aColors); // outputs ["blue", "green", "red"]
Note:
Var aColors = [3, 32, 2, 5];
AColors. sort ();
Alert (aColors); // outputs [2, 3, 32, 5]
This is because numbers are converted to strings and compared by character code.
Splice
Splice: insert data items into the middle of the array
1. Used for deletion: If two parameters are declared, the first parameter is the location of the first item to be deleted, and the second parameter is the number of items to be deleted.
Var aColors = ["red", "green", "blue", "yellow"];
AColors. splice (0, 2 );
Alert (aColors); // outputs ["blue", "yellow"]
2. Used for insertion: When three or more parameters are declared (the second parameter is 0), the data can be inserted at the specified location. The first parameter is the start location, and the second parameter is 0, the third and above parameters are the inserted items.
Var aColors = ["red", "green", "blue", "yellow"];
AColors. splice (2, 0, "black", "white ");
Alert (aColors); // outputs ["red", "green", "black", "white", "blue", "yellow"]
3. Used for deletion and insertion: If three or more parameters are declared (the second parameter is not 0), the data can be inserted at the specified location. The first parameter is the start location, the second parameter is the number of items to be deleted, and the third and above parameters are the inserted items.
Var aColors = ["red", "green", "blue", "yellow"];
AColors. splice (2, 1, "black", "white ");
Alert (aColors); // outputs ["red", "green", "black", "white", "yellow"]
From Artwl