var arr =[1,2,3,4,5,6];
var arrJson=[
{
name:'小花',
age:12,
score:90
},
{
name:'小明',
age:11,
score:60
},
{
name:'小东',
age:15,
score:100
}
]
forEach用法
arr.forEach(element => {
console.log(element)
});
every用法
const result = arr.every( (item,index,array) => {
console.log(item);
console.log(index)
console.log(array);
return item<5
})
console.log('result'+result);
some用法
const result1= arr.some((item,index,array) => {
console.log(item);
console.log(index)
console.log(array);
return item>2
})
console.log('result1'+result1);
filter用法
const result2= arr.filter((item,index,array) => {
return item>3
})
console.log('result2===='+result2)
reduce用法
const sum = arr.reduce((prev,cur,index,arr) => {
console.log(prev,cur,index);
return prev+cur
},-10)
const sum1 = arr.reduce((prev,cur,index,arr) => {
console.log(prev,cur,index);
return prev+cur
})
console.log(sum);
console.log(sum1);
const total=arrJson.reduce((prev,cur,index,arr)=>{
return cur.score+prev
},0)
console.log(total)