We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
var numbers = [1,2,3,4,5]; //item为数组的项 //index为下标 //array为数组对象 var everyResult = numbers.every(function(item,index,array){ return item>2; }) console.log(everyResult);//false
使用箭头符号后
var numbers = [1,2,3,4,5]; var everyResult = numbers.every(item=>(item>2)) console.log(everyResult);//false
var numbers = [1,2,3,4,5]; var everyResult = numbers.some(function(item,index,array){ return item>2; }) console.log(everyResult);//true
var numbers = [1,2,3,4,5]; var everyResult = numbers.some(item=>(item>2)) console.log(everyResult);//true
var numbers = [1,2,3,4,5]; var everyResult = numbers.map(function(item,index,array){ return item>2; }) console.log(everyResult);//[ false, false, true, true, true ]
使用箭头函数后
var numbers = [1,2,3,4,5]; var everyResult = numbers.map(item=>(item>2)) console.log(everyResult);//[ false, false, true, true, true ]
var numbers = [1,2,3,4,5]; var everyResult = numbers.filter(function(item,index,array){ return item>2; }) console.log(everyResult);//[ 3, 4, 5 ]
var numbers = [1,2,3,4,5]; var everyResult = numbers.forEach(item=>(item*2)) console.log(everyResult);//undefined console.log(numbers);//[ 1, 2, 3, 4, 5 ]
var numbers = [1,2,3,4,5]; var everyResult = numbers.reduce((pre,next,index)=>{ return pre*next; }) console.log(everyResult);//120
function myReduce(fn,initItem){//fn为累加函数,initItem为初始值 let arr = this; let i =0;//设置开始索引 let prevItem = null;//设置累加值 if(typeof initItem !=="undefined"){//如果有第二个参数 i=0; prevItem = initItem;//设置初始值 }else{ i=1; prevItem = arr[0]; } for(i;i<arr.length;i++){ if(typeof fn =="function"){ prevItem = fn(prevItem,arr[i],i,arr); } } return prevItem; } Array.prototype.myReduce = myReduce; let a = [1,2,3,4,5]; let c = a.myReduce((pre,next,index)=>{ console.log(index);//0,1,2,3,4 return pre*next; },2); console.log(c);//240
End
by wind-jyf
The text was updated successfully, but these errors were encountered:
No branches or pull requests
数组迭代
every
使用箭头符号后
some
使用箭头符号后
map
使用箭头函数后
filter
forEach
reduce
重写reduce
End
by wind-jyf
The text was updated successfully, but these errors were encountered: