5.2 Array Type
There are two basic ways to create an array:
Method One: Use the array constructor (the new operator can also omit to write).
1 var colors = new Array (); Create an empty array 2 var colors = new Array (20);//create an array with length value of 20 3 var colors = new Array ("Red", "Black", "Blue", "Pinl") ; Create an array containing 3 strings of 4 console.log (colors);
Method Two: Use the array literal notation.
1 var colors = ["Green", "Blue", "pink"]; Create an array of 3 strings with 2 var names = [];//Create an empty array 3 console.log (colors); 4 console.log (names);
var group = [{name: "Lily", age:18}, {name: "Sam", age:20}]; Creates an array that contains two objects. Mgroup (); function Mgroup () {for (var i = 0; i < group.length; i++) { console.log (group[i]); } }
Array of: Index and length
Index of the array:
var colors = ["Green", "Blue", "pink"]; COLORS[3] = "Brown"; When the index value exceeds the number of existing entries in the array, the array is automatically incremented to the length of the index value plus 1 (for example: The index is 3, so the array length is 4) colors[7] = "red";//index is 7 array is 8 console.log ( colors);
Length of array:
When the Length property value is less than the number of array items: Remove var colors = ["Green", "Blue", "pink"];//Create an array containing 3 strings colors.length = 2; The Length property is set to 2 (less than the number of array items) to remove the last item (the one with the position 2), and then access colors[2] will display undefined console.log (colors); Console.log (colors[2]); When the Length property value is greater than the number of array items: Added var colors = ["Green", "Blue", "pink"];//Create an array containing 3 strings colors.length = 4; The Length property is set to 4 (greater than the number of array items), then each new entry will get undefined value console.log (colors); Console.log (Colors[3]); Undefined //Use the Length property to add a new item at the end of the array var colors = ["Green", "Blue", "pink"];//Create an array with 3 strings colors[ Colors.length] = "red"; Add a color in position 3 colors[colors.length] = "BLACK";//Add another color console.log (colors) in position 4; (5) ["Green", "Blue", "pink", "red", "black"]
Because the index value of the last item in the array is always length-1, the position of the next new item is length. Each time an item is added at the end of the array, its Length property is automatically updated to reflect this change.
5.2.1 Detecting arrays
The two methods are:
The problem with the instanceof operator is that it assumes that there is only one global execution Environment if (colors instanceof Array) { console.log ("Yes,array"); } ECMAscript5 new Array.isarray () method. The purpose of this method is to ultimately determine whether a value is an array, regardless of the global execution environment in which it was created. if (Array.isarray (colors)) { Console.log ("is Array"); }
5.2.3 Stack method
The push () method can receive any number of arguments, add them to the end of the array one by one, and return the modified array length.
var colors = new Array (); Create an array of var count = Colors.push ("Red", "Black", "green"); Push into three console.log (colors); Count = Colors.push ("blue"); Push into another console.log (colors);
The Pop () method removes the last item from the end of the array, reduces the length value of the array, and then returns the item that was removed.
var item = Colors.pop (); Get the last item console.log ("The value of the last item of the array is:" + Item); Console.log (colors);
5.2.6 Method of operation
The Concat () method can create a new array based on all the items in the current array. Specifically, this method creates a copy of the current array, adds the received parameters to the end of the copy, and returns the newly constructed array. Without passing arguments to the concat method, it simply copies the current array and returns a copy.
var colors = ["Red", "Blue"]; var colors2 = Colors.concat ("green", "Black", ["Pink", "brown"]); Console.log (COLORS2);
The slice () method, which enables you to create a new array based on one or more items in the current array. The slice () method can accept one or two parameters, that is, to return the starting and ending positions of an item. Note the slice () method does not affect the original array.
- One parameter: the Slice () method returns all items starting at the specified position of the parameter to the end of the current array.
- Two parameters: the Slice () method returns the item between the start and end positions-but not the end position
var colors = ["Orange", "Blue", "green", "Black", "pink", "white", "Brown"); var colors2 = Colors.slice (1); Accept a parameter of var colors3 = Colors.slice (1, 5);//Accept two parameters Console.log (colors2); Console.log (COLORS3);
The splice () method is the most powerful array method, and he has many uses. The main purpose of splice () is to insert items into the middle of the array, but there are 3 ways to use this method:
- Delete: You can delete any number of items by specifying only 2 parameters: the location of the first item to delete and the number of items to delete. For example, splice (0,2) deletes the first two items in the array.
- Insert: You can insert any number of items to a specified location, providing only three parameters: Start position, 0 (number of items to delete), and the item to insert (any number of items).
- Replace: You can insert any number of items at the specified location and delete any number of items at the same time by specifying 3 parameters: The starting position, the number of items to delete, and any number of items to insert. The number of items inserted does not have to be equal to the number of items deleted.
Delete var colors = ["Red", "orange", "Blue"]; var removed = colors.splice (0, 1); Delete the first item console.log (colors); (2) ["Orange", "Blue"] console.log (removed);//["Red"] the returned array contains only one item //Insert var removed = Colors.splice (1, 0, "pink", "white"); Insert two console.log starting from position 1 (colors); (4) ["Orange", "pink", "white", "blue"] console.log (removed);//return an empty array //replace var removed = Colors.splice (1, 1, "Purple", "Black"); Insert two items, delete an item (pink) console.log (colors); ["Orange", "purple", "Black", "white", "blue"] console.log (removed); ["Pink"] returns an array that contains only one item
Learning of arrays (i)