首页 > 其他分享 >数组方法的一些总结

数组方法的一些总结

时间:2022-10-24 22:57:17浏览次数:58  
标签:总结 console 数组 let arr2 arr1 方法 log

数组方法总结

Push方法:

添加一个新元素到数组尾部,并返回修改后数组的长度;

let arr1=[1,2,3,4,5,6,7,8,9]
this.arr1.push("A")
console.log(this.arr1) //1,2,3,4,5,6,7,8,9,A

 

Pop方法:

从数组最后方删除一个元素,并返回移除的项;

let arr1=[1,2,3,4,5,6,7,8,9]
this.arr1.pop()
console.log(this.arr1) //1,2,3,4,5,6,7,8

 

shift方法:

移除数组的第一个元素,并返回该项;

let arr1=[1,2,3,4,5,6,7,8,9]
this.arr1.shift()
console.log(this.arr1) //2,3,4,5,6,7,8,9

 

unshift方法:

数组前端添加一个新元素,并返回修改后的数组长度;

let arr1=[1,2,3,4,5,6,7,8,9]
this.arr1.unshift("A")
console.log(this.arr1) //A,1,2,3,4,5,6,7,8,9

 

splice方法:

删除数组中指定index的指定个元素

splice拥有三个参数

splice('从第几个开始删除','删除多少个','删除后的位置插入的元素') 

实例 从第5个元素开始删除两个元素并插入元素A

let arr1=[1,2,3,4,5,6,7,8,9]
this.arr1.splice(4,2,"A")
console.log(this.arr1) //1,2,3 4,A,7,8,9

 

join方法:

仅接受一个参数,数组转化成字符串 并以 ( ) 内的参数进行分割

let arr1=["A1","B2","C3"];
this.arr1.join(",")     // "A1","B2","C3"

 

concat方法:

在concat的( )中传入参数,使原数组在尾部新增元素, 多用于数组的合并

let arr1=["A1","B2","C3"];
this.arr1.concat("D4")    //["A1","B2","C3","D4"];
let arr2=["E5","H6"]
this.arr1.concat(arr2)//["A1","B2","C3","D4","E5","H6"]

 

slice方法:

从原数组中按照下标提取出新的数组

  • slice拥有两个参数 ( "开始的下标" ,"结束的下标")

  • 当slice("单个参数")是,即为从此下标开始到数组结束进行copy

  • 当slice(0)即为复制原数组

let arr1=["A1","B2","C3","D4","E5","H6"]
this.arr1.slice(1,3)    //["B2","C3","D4"];
this.arr1.slice(4)      //["E5","H6"]
this.arr1.slice(0)      //["A1","B2","C3","D4","E5","H6"]

 

every方法:

数组的每一项都进入条件函数中,当一项的运算结果都为true时,则整个返回true

var arr1=["1","2","3","4","5","6"]
var arr2=arr1.every(function(item,index,array){
return item>3
});
console.log(arr2)// false ,因为1,2,3均返回false

 

some方法:

数组的每一项都进入条件函数中,当一项的运算结果都为true时,则整个返回true

var arr1=["1","2","3","4","5","6"]
var arr2=arr1.some(function(item,index,array){
return item>5
});
console.log(arr2)// true ,因为6大于5 则返回true

 

filter方法:

数组的每一项都进入条件函数中,返回判定为true的元素,组成新的数组

var arr1=[1,2,3,2,5];
var arr2=arr1.filter(function(item,index,array){
return item>=3
});
console.log(arr2)//   [3,5] 过滤掉了小于3的元素

 

map方法:

数组的每一项都进入条件函数中,返回的是每一项运行函数后的值,所新组成的数组,下标具有一一对应的关系

//数组中每一项都 ×5
var arr1=[3,6,9];
var arr2=arr1.map(function(item,index,array){
return item*5
});
console.log(arr2)// [15,30,45]
 

标签:总结,console,数组,let,arr2,arr1,方法,log
From: https://www.cnblogs.com/Dollom/p/16823352.html

相关文章