84669 person learning
152542 person learning
20005 person learning
5487 person learning
7821 person learning
359900 person learning
3350 person learning
180660 person learning
48569 person learning
18603 person learning
40936 person learning
1549 person learning
1183 person learning
32909 person learning
闭关修行中......
咦,这个我不是给你答过了么?:
修正:
好吧,那我们再来改一次试试:
var res = results .reduce(function(p, item) { p.push.apply(p, item.data); return p; }, []) .reduce(function(p, item) { var filters = p.filter(function(data) { return data.code === item.code; }); if (filters.length === 0) { p.push(item); } else { filters[0].poll += item.poll; } return p; }, []); console.log(res); var max = res .reduce(function(p, item) { if (p.poll < item.poll) { return item; } return p; }, {poll: 0}); console.log(max); //你想要的结果: { code: '574417bc79df540065d92df7', poll: 18 }
当然我这般似乎不用ES6,真的不怎么简洁
ES6
深度固定,属性固定。直接用for扫描一遍不就得了……ans就是所求。
ans
const map = {}; let max = 0, ans; for(let i = 0; i < results.length; i++) { for(let j = 0; j < results[i].data.length; j++) { map[results[i].data[j].code] = map[results[i].data[j].code] ? map[results[i].data[j].code] + results[i].data[j].poll : results[i].data[j].poll; if(map[results[i].data[j].code] > max) { max = map[results[i].data[j].code]; ans = results[i].data[j].code; } } }
@leftstick 既然res是个数组,用sort就可以
res
sort
console.log(res.sort(function (cur, next) { return cur.poll < next.poll })[0]) // { code: '574417bc79df540065d92df7', poll: 18 }
咦,这个我不是给你答过了么?:
修正:
好吧,那我们再来改一次试试:
当然我这般似乎不用
ES6
,真的不怎么简洁深度固定,属性固定。直接用for扫描一遍不就得了……
ans
就是所求。@leftstick 既然
res
是个数组,用sort
就可以