var obj=Array.prototype.reduce.call(str,function(pre,cur){
pre[cur]?pre[cur]++:pre[cur]=1;
return pre;
},{});
console.log(obj);
//{a: 4, b: 1, c: 3, d: 3, e: 3, …}
var arr=[1,2,3,4,5,6];
var sum=arr.reduce(function(pre,cur,index,arr){
console.log(index);//当前项的下标
return pre+cur;
});
console.log(sum);//21
var result = [
{
subject: 'math',
score: 88
},
{
subject: 'chinese',
score: 95
},
{
subject: 'english',
score: 80
}
];
var totalScore=result.reduce(function (pre,cur) {
return pre+cur.score;
},0);
console.log(totalScore);
欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) | 黑马程序员IT技术论坛 X3.2 |