1.for循环 (无返回值)
<script>
let arr2 = ['张三','李四','王五']
for (let i=0;i<arr2.length;i++){
console.log(i,arr2[i])
}
</script>
2.forEach() (无返回值)
<script>
let arr = [1,2,3,4,5,6];
let res = 0;
arr.forEach(function(item,index){
item>3?res++:res
})
console.log(res)
console.log(arr)
</script>
3.map() (返回一个新的数组)
<script>
let arr = [1, 2, 3, 4, 5, 6]
let res = arr.map((item, index) => {
return item * 3;
})
console.log(res)
console.log(arr)
</script>
4.some() (返回布尔值)
<script>
let arr = [1,2,3,4,5,6,7];
let ressome = arr.some((item,index)=>{
return item>5
})
console.log(ressome);
if(ressome){
console.log('有一个是正确的我就是true')
}else{
console.log('全部是错误的我就是false')
}
</script>
5.erery() (返回布尔值)
<script>
let arr = [1,2,3,4,5,6];
let resevery = arr.every((item,index)=>{
return item*index;
})
console.log(resevery);
if(resevery){
console.log('全部是正确的即为true')
}else{
console.log('有一个是错误的即为false')
}
</script>
6.filter() (返回符合条件的新数组)
<script>
let arr = [2,4,6,8,10,12];
let res = arr.filter((item,index)=>{
return item+1>=8;
})
console.log(res);
console.log(arr);
</script>
7.for of 循环
<script>
let arr = [1, 2, 3, 4, 5, 6, 7];
for (let val of arr) {
console.log(val)
}
for (let [index, val] of arr.entries()) {
console.log(index, val)
}
</script>
8.reduce() (有返回值)
<script>
debugger;
let arr = [1,2,3,4,5,6,7];
let resreduce = arr.reduce((val,item,index)=>{
return val+=10;
},10)
console.log(resreduce);
console.log(arr);
</script>
9.find()(返回符合条件的第一个元素如没有则返回undefined)
<script>
let arr3 = [1,2,3,4,5,6,7,8]
let arr4 = arr3.find(function(item,index){
console.log(item,index)
return item>1
})
console.log(arr4);
</script>
10.findIndex() (返回第一个满足条件的索引若没有则返回-1)
<script>
let arr5 = [1,2,3,4,5,6,7,8]
let arr6 = arr3.findIndex(function(item,index){
return item>1
console.log(item,index)
})
console.log(arr6);
</script>