Creating an array of ways:
One way: Construction of the array constructor
var arr = new Array (); if the argument passed to a number that represents the length of the array, with no content
// can pass strings and numbers, separated by commas, as the contents of the array.
Method two: literal method creates an array (Typically, this method)
var arr = []; // may write to directly. Separated by commas
The basic method of operation of the array:
table of Contents:
- push
- pop
- unshift
- shiift
- concat
- join
- splice
- slice
- indexOf
- lastIndexOf
push () method
Var len = Arrkpus ()
Add one or more new elements at the end of the array, and returns the new length of the array.
Native JS reconstruction push method
Push function. 1 (ARR) { 2 . 3 for (I =. 1; I <The arguments.length; I ++) { . 4 . 5 ARR [arr.length] = arguments [I]; // add tail array incoming parameters passed 6 }. 7 . 8 . 9 arr.length return; // returns the length of the array 10 . 11} 12 is 13 is NUM = var Push (ARR,. 7,. 8,. 9) 14 15 the console.log (ARR, I)
pop () method
arr.pop are thrust = ()
To delete one or more elements of the end of the array, and returns this element is deleted
Native JS pop reconstruction method
POP function. 1 (ARR) { 2 . 3 NUM = var ARR [arr.length -. 1]; // save the last element of the array . 4 . 5 arr.length--; // delete the last element . 6 . 7 // return return NUM deleted elements . 8 . 9} 10 . 11 POP (ARR)
unshuift () method
// one or more new elements are added on top of the array, return the new length of the array
was len = arr.unshift ()
Native JS reconstruction method unshift
The unshift function. 1 (ARR) { 2 . 3 The arguments.length var len = -. 1; // length defined to add elements, . 4 . 5 for (var = arr.length I -. 1; I> = 0; i--) { // len rearward movement of the entire array units . 6 . 7 ARR [I + len] = ARR [I]; . 8 . 9 // the console.log (ARR) 10 . 11} 12 is 13 is for (var. 1 = J; J <arguments .length; j ++) {// the parameters passed into the top of the array in the order 14 15 ARR [J -. 1] = arguments [J] 16 . 17 the console.log (ARR) 18 is . 19} 20 is 21 is return arr.length // returns the new length of the array 22 is 23} 24 25 unshift(arr, 9, 8, 7) 26 27
shift () method
// Remove the first element of the array and returns that element.
arr.shift are thrust = ()
Native JS reconstruction shift method:
Shift function. 1 (ARR) { 2 . 3 = var ITME ARR [0]; // Get the first element . 4 . 5 // the console.log (ITME) . 6 . 7 for (I = 0; I <arr.length; I ++) {// the length of a whole array moves forward . 8 . 9 ARR [I] = ARR [I +. 1] 10 . 11 the console.log (ARR) 12 is 13 is} 14 15 // return ITME return the first element of the array 16 . 17 } 18 is . 19 the console.log (Shift (ARR))
concat () method
var arr1 = arr.concat ( 10,11,12)
var arr2 =arr.concat ( arr1 )
// add a new element to the original array or connect multiple arrays, copied to a new array
Native JS reconstruction concat method:
The concat function. 1 (ARR) { 2 . 3 Array var = []; // define an empty array to copy new array . 4 . 5 var NUM = 0; // index of the new array . 6 . 7 for (var I = 0; I <arr.length; i ++,) { // copy the first array into a new array ++ NUM . 8 . 9 array [NUM] = ARR [I] 10 . 11} 12 is 13 is for (var. 1 = J; J <the arguments.length ; j ++, num ++) { // copied to the new array parameters 14 15 iF (arguments [J] .constructor === the array) {// check whether it is an array type 16 . 17 for (var m = 0; m < arguments [j] .length; m ++ , num ++) {// iterate array of parameters, to the new array 18 is . 19 array [NUM] = arguments [J] [m] 20 is } 21 is 22 is 23 is due to the above two cycles num-- // new array subscripts added 2 times, so that a reduction 24 25} instead of the else {// array type parameters, the parameter to the new array 26 is 27 Array [NUM] = arguments [J] 28 29} 30 31 is} 32 33 is return // array returns a new array 34 is 35}
join () method
var str = arr.join( )
join () method for all elements in the array into a string.
Elements are separated by a specified delimiter. If you do not fill in the parameters, separated by commas default
splice () method
arr.splice (from any starting position, remove the number of elements, added elements ......)
// array to add, delete, replace one or more elements, returns the deleted array elements
var arr1 = arr.splice () // create an empty array arr1
var arr1 = arr.splice (0) arr remove all elements, all the elements to pass of arr1,
var arr1 = arr.splice (-1) is negative the parameters passed when the reciprocal of the first several starts.
var arr1 = arr.splice (2,0,12,14) indicates the second bit first, without deleting the element 12 and insert 14
// (positive element is inserted backwards, the negative element is inserted forward)
slice () method
Function: intercepting array specified position, and returns an array taken, does not change the original array
Parameters: slice (startIndex, endIndex)
Note: You can return the selected elements from the existing array. The method takes two parameters slice (start, end), the last one are omitted;
start and end can be thought negative, indicating the start counting from the last one, the last as -1 indicates a negative number.
IndexOf () method
Function: The specified data, from front to back, the position of the query appear in the array, if the specified data is not present, return -1, finds the specified index data is returned to the data
Parameters: indexOf (value, start); value for the data to be queried; start is optional, indicates the position of the beginning of the query, when the start is negative, the number of forward from the end of the array; if the query the existence of the value of the method returns -1
Note: If the data is found, immediately returns the index of the data, no longer continue to look back
lastIndexOf () method
Function: The specified data, from the back, the position of the query appear in the array, if the specified data is not present, return -1, finds the specified index data is returned to the data
Parameters: indexOf (value, start); value for the data to be queried; start is optional, indicates the position of the beginning of the query, when the start is negative, the number of forward from the end of the array; if the query the existence of the value of the method returns -1