一,数组的查询
1,返回第一个满足条件的元素索引值:findIndex
findIndex() 方法传入一个测试条件(回调函数,参数是三个:value,index,arr:被查找的数组),返回符合条件的数组第一个元素位置(index),如果没有符合条件的元素返回 -1。
例子:
const list=[
{
"id": 0,
"info": "Racing car sprays burning fuel into crowd.",
"done": false
},
{ "id": 1, "info": "Japanese princess to wed commoner.", "done": false },
{
"id": 2,
"info": "Australian walks 100km after outback crash.",
"done": false
},
{ "id": 3, "info": "Man charged over missing wedding girl.", "done": false },
{ "id": 4, "info": "Los Angeles battles huge wildfires."," done": false }
]
const listIndex=list.findIndex(item=>item.id==2)
console.log(listIndex) //2
2,返回第一个满足条件的元素值:find
该方法主要应用于查找第一个符合条件的数组元素。它的参数是一个回调函数。在回调函数中可以写你要查找元素的条件,当条件成立为true时,返回该元素。如果没有符合条件的元素,返回值为undefined。
回调函数的参数也是三个:value,index,arr:被查找的数组
例子:
const list=[
{
"id": 0,
"info": "Racing car sprays burning fuel into crowd.",
"done": false
},
{ "id": 1, "info": "Japanese princess to wed commoner.", "done": false },
{
"id": 2,
"info": "Australian walks 100km after outback crash.",
"done": false
},
{ "id": 3, "info": "Man charged over missing wedding girl.", "done": false },
{ "id": 4, "info": "Los Angeles battles huge wildfires."," done": false }
]
// const listIndex=list.findIndex(item=>item.id==2)
// console.log(listIndex) //2
const listItem=list.find((value,index,arr)=>{
//回调函数,用了{}就需要return了
return value.info=="Australian walks 100km after outback crash."
})
console.log(listItem) //对应的对象
3,返回所有满足条件的元素构成的数组:filter()
filter()与find()使用方法也相同。同样都接收三个参数。不同的地方在于返回值。filter()返回的是数组,数组内是所有满足条件的元素,而find()只返回第一个满足条件的元素。如果条件不满足,filter()返回的是一个空数组,而find()返回的是undefined。
例子:
const list=[
{
"id": 0,
"info": "Racing car sprays burning fuel into crowd.",
"done": false
},
{ "id": 1, "info": "Japanese princess to wed commoner.", "done": false },
{
"id": 2,
"info": "Australian walks 100km after outback crash.",
"done": false
},
{ "id": 3, "info": "Man charged over missing wedding girl.", "done": false },
{ "id": 4, "info": "Los Angeles battles huge wildfires."," done": false }
]
const listArr=list.filter((value,index,arr)=>{
//回调函数,用了{}就需要return了
return value.id>=2
})
console.log(listArr) //对应的对象
返回结果:
4,返回元素在数组中的位置(不存在则返回-1)
var arr=[1,2,3,4,5]
console.log(arr.indexOf(5)) //4
console.log(arr.indexOf(6)) //-1
二,数组的遍历
1,普通for循环
for(j = 0; j < arr.length; j++) {
}
2,优化版for循环(推荐)
for(j = 0,len=arr.length; j < len; j++) {
}
3,foreach循环(推荐)
arr.forEach(function(e){
});
4,forin循环
for(j in arr) {
}
5,map遍历
arr.map(function(n){
})
三,常用数组的操作方法
1,arr.push(1,2,3)
在数组末尾依次插入多个,返回插入后的数组长度
2,arr.pop()
移除末尾的一个元素,返回移除的元素
3,arr.shift()
从数组头部取下一个元素,返回值就是这个元素
4,arr.unshift(1,2,3)
给数组头部按顺序插入【1,2,3】,返回插入后的数组长度
5,arr.concat(arr2)
合并数组,返回合并好的新数组(源数组保持不变)
6,arr.slice(start,end)
从数组的指定区间(索引值,且不包含最后一个)拿出来创建新数组并返回,不改变原数组
7,arr.splice(start,length,元素……)
从start处开始的length个元素删除,再插入元素,会修改源数组
1,arr.splice(index,1),删除索引为index的那个元素
8,arr.join(拼接符)
使用字符串将数组中的元素拼接成字符串,返回字符串
9,arr.reverse()
将数组从大到小排列
10,arr.sort()
将数组按位从小到大排列
四,数组的es5方法
1,every()
对数组中的每一项运行给定函数,只有每一项都返回true时,才返回true,可以用来对数组的每一项进行某种条件的判断。判断它是不是都满足某个条件
var arr=[1,2,3,4,5,6,7]
var flag=arr.every((item,index,array)=>{
return item>0 //每一项执行之后,都返回true
})
console.log(flag) //true
2,some()
对数组中的每一项运行给定函数,只要有一项返回true时,就返回true,可以用来对数组的每一项进行某种条件的判断。判断它是不是至少存在一项满足某个条件
var arr=[1,2,3,4,5,6,7]
var flag=arr.some((item,index,array)=>{
return item>6
})
console.log(flag) //true
3,filter()
对数组中的每一项执行给定的函数,然后返回该函数会返回true的项组成的数组。也就是刷选出满足某个条件所有项。
var arr=[1,2,3,4,5,6,7]
var newArr=arr.filter((item,index,array)=>{
return item>5
})
console.log(newArr) //[6,7] 满足条件的就这两个
4,map()
对每一项进行某种处理,然后返回处理后的结果数组。适用在需要对数组的每一项做统一的处理时。
当数组元素是基本数据类型时,map()方法不会改变原数组;当数组元素是引用类型时,map()方法会改变原数组。
var arr=[1,2,3,4,5,6,7]
var newArr=arr.map((item,index,array)=>{
var a=item+2
return a
})
console.log(newArr) //[3,4,5,6,7,8,9] //将每一项的返回值手记到一个新数组
5,forEach()
对数组中的每一项运行给定函数,它没有返回值,不能return,适用于对数组的每一项都进行某种处理。
var arr=[1,2,3,4,5,6,7]
arr.forEach((item,index,array)=>{
var a=item+2
console.log(a) //3,4,5,6,7,8,9
})
需要注意的是foreach里面不能return ,不能使用async和await,不会同步执行,建议改用for循环,具体的可以看foreach的源码。