Create an array
- Each entry in the array can hold any type of data, and the size of the array can be dynamically adjusted, growing automatically as data is added to accommodate new data.
- Create an Array using the Array constructor
var arr = new Array(); <! Pass a value to the constructor, and the array is automatically changed to the value of the array length property --> <! Var arr = new Array(20)Copy the code
- Create arrays using literals. As with objects, the Array constructor is not called when literal notation is used
<! -- array literals are represented by a pair of square brackets containing items separated by commas --> var arr =[1,2,3]; var names = []; // Create an empty arrayCopy the code
Second, the array value read and set
Var num = [1, 2, 3, 4]; Alert (num[0]) // display first item --> 1 num[2] ='red'; Num [4] = 5; // Add the fourth itemCopy the code
The index in square brackets indicates the value to be accessed
- If the index is less than the number of items in the array, the corresponding item value is returned.
- Changing the value replaces the value at the specified location.
- If the index is greater than the number of items in the array, the array automatically increases to the length of the index value, adding new items. You can remove items from or add new items to the end of the array using the array’s Length property
<! Var colors=['red'.'yellow'.'blue'];
colors.length = 2;
alert(colors[2]) //undefined
alert(colors) // ['red'.'yellow'] <! Var num = [1,2,3,4]; num.length = 5; alert(num[4]) //undefinedCopy the code
3. Conversion method
toString()
- Calling the toString() method of the array returns a comma-separated string for each value in the array
Var num = [1, 2, 3, 4]; alert(num.toString()) //'1, 2, 3, 4'
Copy the code
join()
- The join() method can use different delimiters to build strings.
- The join() method takes only one argument, a string used as a delimiter. No values are separated by commas by default
var colors=['red'.'blue'.'green'];
alert(colors.join()) //'red,blue,green'
alert(colors.join('| |')) / /'red||blue||green'
Copy the code
If an item in the array is null or undefined, the value is represented as an empty string in the result returned by join(), toString(), toLocaleString(), valueOf().
Array methods
- The push() method can take any number of arguments, append them one by one to the end of the array, and return the modified array length.
var num =[];
var num1 = num.push(1,2)
alert(num) // [1,2]
alert(num1) //2
Copy the code
- The pop() method removes the last item from the end of the array, reducing the length value of the array and returning the removed item.
Var num = [1, 2, 3, 4]; Var num1 = num.pop() alert(num) // [1,2,3] alert(num1) // 4Copy the code
- The shift() method removes the first item of the array and returns the change, reducing the array length by one.
Var num = [1, 2, 3, 4]; Var num1 = num.shift() alert(num) //[2,3,4] alert(num1) // 1Copy the code
- The unshift() method adds any item to the front of the array and returns the new array length.
Var num = [1, 2, 3, 4]; Var num1 = num. Unshift (5, 6); // [1,2,3,4,5,6] alert(num1) //6Copy the code
- The reverse() method reverses the order of the items and returns the sorted array.
Var num = [1, 2, 3, 4]; Num. Reverse () / /,3,2,1 [4]Copy the code
- The default sort order of the sort() method is based on the string UniCode code. Because sorting is done in the order of the string UniCode code, the array elements should first be converted to strings (if necessary) for comparison.
Grammar: arrayObject. Sort (sortby);
The sortby argument is optional and specifies the sort order, but it must be a function.
<! Var arr = [var arr = ['tom'.'ani'.'love'.'dav'];
arr.sort() // ['ani'.'dav'.'love'.'tom'] <! Var num = [0,1,5,10,15]; Num. Sort () / /,1,10,15,5 [0] <! Var num = [0,1,5,10,15]; var snum = num.sort(function(a,b){
returna-b; }) alert(snum) // [0,1,5,10,15] <! Var num = [0,1,5,10,15]; var snum = num.sort(function(a,b){
return b-a; }) alert (snum) / /,10,5,1,0 [15]Copy the code
- The concat() method copies the current array, adds the received arguments to the end of the copied array, and returns the newly constructed array
Var num = [1, 2, 3]; var num2 = num.concat('red', (4, 5)) alert (num) / / [1, 2, 3] alert (num2) / / [1, 2, 3,'red', 4, 5)Copy the code
- The slice() method creates a new array based on one or more items in the current array. This method takes two parameters, the start and end positions of the return item.
With only one argument, the slice() method returns all items from the start to the end of the array.
If there are two arguments, the slice() method returns the entry between the start and end positions, excluding the entry at the end position.
The slice() method does not affect the original array
If there is a negative number in the slice() method, the position is determined by adding the array length to the negative number
If the end position is less than the start position, an empty array is returned
Var num = [1, 2, 3, 4, 5]; Var num1 = num. Slice (1) //[2,3,4] var num2 = num. Slice (1,4) //[2,3,4] var num3 = num. Slice (-4,-1) //[2,3,4] var num4 = num. Slice (2, 1) / / []Copy the code
- The splice() method always returns the deleted items, or an empty array if no items are deleted
- Delete: Can delete any number of items, specify two parameters: the first item location to delete and the number of items to delete
Var num = [1, 2, 3, 4]; Var remove = splice (1, 2) / / [2, 3] alert (num) / / [1, 4]Copy the code
- Insert: You can insert any number of items into the specified position, specifying three arguments: the start position,0(number of items to delete), and the item to insert
Var num = [1, 2, 3, 4]; Var num2 = num. Splice (1,0,5,6) / / [] alert (num) / /,5,6,2,3,4 [1]Copy the code
- Replace: You can replace any item with a specified position, specifying three parameters: the start position, the number of items to delete, and the item to insert. The inserted item may not be the same as the deleted item.
Var num = [1, 2, 3, 4]; Var num2 = num. Splice (1,2,5,6,7) / / [2, 3] alert (num) / /,5,6,7,3,4 [1]Copy the code
- Both methods,indexOf() and lastIndexOf(), take two arguments: the item to look for and (optionally) the index at the start of the search,indexOf() looks back at the first occurrence, and lastIndexOf() looks back. Returns -1 if not found, returns the current item’s index in the array if found
Var num =,2,3,4,5,4,3,2,1 [1]; num.indexOf(4); // 3 num. LastIndexOf (4) // 5 num. LastIndexOf (4,4) // 3 num. LastIndexOf (0) // -1Copy the code
ES5 method
- Every () and some() are used to check whether items in an array satisfy certain conditions
- Every () must return true for every item, and false otherwise
- Some () returns true as long as one of the functions passed by some() returns true.
Var num =,2,3,4,5,4,3,2,1 [1]; var num1 = num.every(function(item,index,array){
return item > 2;
})
alert(num1) // false
var num2 = num.some(function(item,index,array){
return item > 2;
})
alert(num2) // true
Copy the code
- Filter () filters out the items that meet the conditions to construct a new array.
Var num =,2,3,4,5,4,3,2,1 [1]; var num2= num.filter(function(item,index,array){
returnItem > 2}) alert(num2) //Copy the code
- Map () returns a new array whose elements are the values processed by the original array elements.
Var num =,2,3,4,5,4,3,2,1 [1]; var num2 = num.map(function(item,index,array){
returnItem * 2}) alert(num2) // [2,4,6,8, 8,6,4,2]Copy the code
- ForEach () iterates through arrays just like the for loop
- Reduce () and reduceRight() both iterate over all the items in the array and return a final value. Reduce () starts from the first item and reduceRight() starts from the last item. Takes four parameters: the previous value, the current value, the index of the item, and the array object. The first iteration occurs on the second item of the array, so the first argument is the first item of the array, and the second argument is the second item of the array.
Var num = [1, 2, 3, 4, 5]; var sum = num.reduce(function(prev,cur,index,array){
return prev + cur;
});
alert(sum) // 15
Copy the code