Summary of Array Array Learning in JS

  • 2021-07-12 05:07:59
  • OfStack

Reference types are divided into Object type (so-called object), Array type (array discussed in this article), Function type and so on.

So, what do arrays do? In my opinion, it is used to save data.

1. Declare an array:

1. The constructor var colors=new Array (); In abbreviation, new can be omitted, that is, var colors=Array ();

2. Array literals var colors = ["black", "green", "pink"];

2. Read and set the value of the array:

Read: colors [x]; Parameters x are 0 ~ colors. length-1;

If you set it, just assign it directly to colors [x] =, which will overwrite the previous value;

3. Here, briefly talk about the usage of length:

colors. length Gets the length of the array. It can also be said that the array has several items. If an array has 7 items, but you write colors. length = 2, the following 5 items will be deleted;

The length attribute can also be used to add data to the end of the array: colors [colors. length] = for assignment;

4. Actions in an array:

方法 作用 返回值
Array.push(x,y,z) 把xyz添加到数组末尾 新数组长度
Array.pop() 移除数组最后1项 移除的最后1项
Array.shift() 移除数组第1项 移除的第1项
Array.unshift(a,b,c) 在数组前端添加a,b,c 新数组长度
Array.reverse() 反转数组 反转后的新数组
Array.sort() 对数组中每1项的字符串进行升序排列 重新排序后的数组
Array.concat(a,b,c) 连接数组 返回连接好的新数组
Array.slice(1,n) 截取数组,从1到n,1和n为索引值 返回截取的数组(在这里返回从1开始,到n之前结束)
Array.indexOf(a,start) 查找a的所在的位置,从start开始 返回a所在的索引值,如果没有查找到则返回-1
Array.lastIndexOf(a,atart) 与indexOf相反,lastIndexOf从末尾开始查找 返回a所在的索引值,如果没有查找到则返回-1

Take out the splice () method list. Why did you take it out? Because it's awesome;

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

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

2. Insert. Accept 3 parameters: starting position, 0, and the item to insert.

Example: splice (2, 0, "red", "green"), inserts red and green at the position where the array index value is 2.

3. Replace. Accept 3 parameters: starting position, number of items to delete, and items to insert.

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


tips : sort () Usage example: Array in ascending order 
function compare(val1,val2){
if(val1<val2){
return -1;
}else if(val1>val2){
return 1;
}else{
retuen 0
}
}
var num=[0,2,9,3,1];
num.sort(compare);
alert(num);//0,1,2,3,9

5. Iterative methods in arrays

1. every () and some ():


 Query every in the array 1 Item meets the conditions, if every 1 Items all return true , then result Return true . 
var numbers=[0,1,2,3,4];
var result=numbers.every(function(item,index,array){
return (item>2)
})
alert(result);//false
 Query every in the array 1 Item meets the conditions, and if so 1 Items all return true , then result Return true . 
var numbers=[0,1,2,3,4];
var result=numbers.some(function(item,index,array){
return (item>2)
})
alert(result);//true

2.filter():

This method returns an array of items whose result is true;

3.map():


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

Returns a new array after the array has finished executing parameters.

6. Merging

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;

The first time the function is executed, prev is 1, cur is 2, and the second time the function 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.


Related articles: