1. Creation of arrays
1 var New // Create an array 2 var New // creates an array and specifies a length of ten 3 var New // Create an array and assign a value
2. Access to array elements
1 varMycars =NewArray (3);2Mycars[0] = "Saab";//assigning values to array elements3MYCARS[1] = "Volvo";//assigning values to array elements4MYCARS[2] = "BMW";//assigning values to array elements5 varArrayobj =NewArray ("A", "B", "C");//Create an array and assign a value6 varBValue = arrayobj[1];//gets the element value of the array
3. Addition of array elements
The push () method adds one or more elements to the end of the array and puts back the new length
1 var New Array (); 2 arr[0] = "Xiaohong"; 3 arr[1] = "Xiaobai"; 4 arr[2] = "Xiaoli"; 5 document.write (arr + "<br/>"); 6 document.write (Arr.push ("Xiaoliu" + "<br/>"); 7 document.write (arr);
Output Result:
Xiaohong,xiaobai,xiaoli
4
Xiaohong,xiaobai,xiaoli,xiaoliu
The Unshift () method adds one or more elements to the beginning of the array and returns the new length
1 var New Array (); 2 arr[0] = "Xiaohong"; 3 arr[1] = "Xiaobai"; 4 arr[2] = "Xiaoli"; 5 document.write (arr + "<br/>"); 6 document.write (Arr.unshift ("Xiaoliu" + "<br/>"); 7 document.write (arr);
Output Result:
Xiaohong,xiaobai,xiaoli
4
Xialiu,xiaohong,xiaobai,xiaoli
The splice () method adds/removes items to/from the array and then puts back the deleted items
Grammar:
Arrayobj.splice (index,howmany,item1,item2,... itemn);
Index: Specifies where to add/remove items, using negative numbers to specify the position at the end of the array.
Howmany: The number of items to delete. If set to 0, the item is not deleted.
ITEM1,ITEM2,... itemn: Adds a new item to the array.
1 varArrNewArray (6);2Arr[0] = "1";3ARR[1] = "11";4ARR[2] = "111";5ARR[3] = "1111";6ARR[4] = "11111";7ARR[5] = "111111";8document.write (arr + "<br/>");9document.write (2,0, "AAAAA");Tendocument.write (arr + "<br/>");
Output Result:
1,11,111,1111,11111,111111
1,11,aaaaa,111,1111,11111,111111
4. Array element deletion
Arrayobj.pop (); Removes the last element and returns the element value
Arrayobj.shift (); Removes the first element and returns the element value, and the elements in the data are automatically moved forward
1 var a = [1,2,3,4,5]; 2 var b = A.shift (); // shift Deletes the first item of the original array and returns the value of the deleted element; returns undefined if the array is empty 3 // a = 2,3,4,5 b = 1
Arrayobj.splice (); See above splice () description
5. Interception and merging of arrays
The slice () method returns the selected element from an existing array. This method extracts a portion of a string and returns the extracted part with a new string.
The slice () method does not change the original array.
Array.slice (Start,end);
Start: Specifies where to start the selection.
End: Specifies where to end the selection.
JavaScript array manipulation