I am a little white (really a little white) and I am studying the front-end by myself. Next I will talk about some Array Operations I have summarized during the learning process. If there are any errors, please let me know more about them, small open-minded acceptance. The first time I wrote a blog... A bit square...
I am a little white (really a little white) and I am studying the front-end by myself. Next I will talk about some Array Operations I have summarized during the learning process. If there are any errors, please let me know more about them, small open-minded acceptance.
The reference types include Object type (so-called Object), Array type (Array discussed in this article), and Function type.
So what is the array? In my opinion, it is used to save data.
I. Declare an array:
1. constructor var colors = new Array (); in short, new can be omitted, that is, var colors = Array ();
2. array literal var colors = ["black", "green", "pink"];
2. Read and set the value of the array:
Read: colors [x]; parameter x is 0 ~ Colors. length-1;
If this parameter is set, assign a value to colors [x] =, which overwrites the previous value;
Iii. Here I will briefly describe the usage of length:
Colors. length is used to obtain the length of an array. It can be said that there are several items in the array. If there are seven items in an array, but you write colors. length = 2, the next five items will be deleted;
The length attribute can also be used to add data to the end of the array: colors [colors. length] =;
4. Operations in the array:
Method |
Function |
Return Value |
Array. push (x, y, z) |
Add xyz to the end of the array |
Length of the new array |
Array. pop () |
Remove the last entry of the array. |
Last Deleted item |
Array. shift () |
Remove the first entry of the array. |
The first item to be removed |
Array. unshift (a, B, c) |
Add a, B, c |
Length of the new array |
Array. reverse () |
Reverse Array |
New array after Inversion |
Array. sort () |
Sort the strings of each item in the array in ascending order. |
Array after sorting |
Array. concat (a, B, c) |
Connection Array |
Returns the newly connected array. |
Array. slice (1, n) |
Truncate the array, from 1 to n, 1 and n are the index values. |
Return the truncated array (here, the return value starts from 1 and ends before n) |
Array. indexOf (a, start) |
Locate the location of a, starting from start |
Returns the index value of a. If no index is found,-1 is returned. |
Array. lastIndexOf (a, atart) |
In contrast to indexOf, lastIndexOf searches from the end |
Returns the index value of a. If no index is found,-1 is returned. |
The splice () method ticket is provided. Why do I obtain the ticket? Because it is awesome;
1. Delete. Two parameters are accepted: the location of the first item to be deleted and the number of items to be deleted;
For example, splice () means to delete the two or three items of the array;
2. insert. Three parameters are accepted: Start position, 0, the item to be inserted.
For example, splice (, "red", "green") inserts red and green at the position where the array index value is 2.
3. replace. Three parameters are accepted: Start position, number of items to be deleted, and items to be inserted.
For example: splice (, "red", "green"), delete the index value of 2, add red and green.
(val1< -1 (val1> 10
5. Iteration Method in array
1. every () and some ():
numbers=[0,1,2,3,4 result=numbers.every( (item>2 numbers=[0,1,2,3,4 result=numbers.some( (item>2
2. filter ():
This method returns an array composed of items whose results are true;
3. map ():
var result=numbers.map(function(item,index,array){return item*2;})
Returns the new array after the parameters are executed.
Sat. Merge.
Array. reduce ()
var numbers=[1,2,3,4,5];var sum=numbers.reduce(function(prev,cur,index,array){return prev+cur})alert(sum);
In the previous example, reduce () accepts four parameters. The first parameter is the first item of the array, and the second parameter is the second item of the array;
For the first execution of the function, the prev is 1, the cur is 2, the second execution, the prev is 3 (the result of 1 + 2), and the cur is 3.
Array. reduceRight (). Similar to reduce. It only starts from the right side of the array.
The above is the summary of Array learning in Js. For more information, see PHP Chinese Network (www.php1.cn )!