Home  >  Article  >  Web Front-end  >  Summary of Array array learning in JS

Summary of Array array learning in JS

黄舟
黄舟Original
2017-02-10 10:04:211008browse

First time writing a blog. . . A bit square. . .

I am a novice (a real novice), self-study of front-end. Now I will talk about some array operations that I summarized during the learning process. If there are any mistakes, please point them out. Humbly accept.

Reference types are divided into Object types (so-called objects), Array types (arrays discussed in this article), Function types, etc.

So, what does an array do? It seems to me that it is used to save data.

1. Declare an array:

1. Constructor var colors=new Array(); for abbreviation, you can omit new, 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 Here is a brief talk about the usage of length:

colors.length gets the length of the array, which can also be said to be the number of items in the array. If an array has 7 items, but you write colors.length=2, Then the next 5 items will be deleted;

Using the length attribute, you can also add data to the array at the end: colors[colors.length]=for assignment;

4. Operations in the array:

MethodFunctionAdd xyz to the end of the array##Array.pop()Remove the end of the array One itemRemove the last itemArray.shift()Remove the first item of the arrayShift The first item to be dividedArray.unshift(a,b,c)Add a,b,c at the front of the arrayNew array length Array.reverse()Reverse arrayThe new array after reversal Array.sort()Arrange the strings of each item in the array in ascending orderReordered arrayArray.concat( a,b,c)Connect arraysReturn the connected new arrayArray.slice(1,n)Intercept the array, from 1 to n, 1 and n are the index valuesReturn the intercepted array (return here starts from 1 and ends before n)Array.indexOf(a, start)Find the location of a, starting from startReturn the index value of a, if not found, return -1Array.lastIndexOf(a,atart)Contrary to indexOf, lastIndexOf searches from the end Returns the index value where a is located, if it is not found, it returns -1
Return value Array.push(x,y, z)
New array length

The splice() method is listed here. Why take it out alone? Because it’s awesome;

1. Delete. Accepts two parameters: the position of the first item to be deleted and the number of items to be deleted;

Example: splice(1,2), which is to delete items 2 and 3 of the array;

2. Insert. Accepts three parameters: the starting position, 0, and the item to insert.

Example: splice(2,0,"red","green") will insert red and green at the array index value 2.

3. Replacement. Accepts three parameters: starting position, number of items to delete, and items to insert.

Example: splice(2,1,"red","green"), delete the item with index value 2, and add red and green.

(val181f0d8c0f18b4c7f4121119843be84c9 10


Five. Iteration methods in arrays

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 will return an array composed of items whose result is true;

3.map():

var result=numbers.map(function(item,index,array){
return item*2;
})

Returns the new array after the array executes the parameters.

six. 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, the second parameter is the second item of the array;

The first time the function is executed, prev is 1, cur is 2, the second time it is executed, prev is 3 (The result of 1+2), cur is 3.

Array.reduceRight(). Similar to reduce. Just start from the right side of the array.

The above is the summary of Array array learning in Js. For more related content, please pay attention to the PHP Chinese website (www.php.cn)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn