📌  相关文章
📜  对象获取具有最大值的属性 javascript 代码示例

📅  最后修改于: 2022-03-11 15:02:22.045000             🧑  作者: Mango

代码示例2
let objects = [{id: 0, votes: 5}, {id: 1, votes: 3}, {id: 2, votes: 11}]

let maxObj = objects.reduce((max, obj) => (max.votes > obj.votes) ? max : obj);

/* `max` is always the object with the highest value so far. 
 * If `obj` has a higher value than `max`, then it becomes `max` on the next iteration.
 * So here:
 *  |  max = {id: 0, votes: 5},   obj = {id: 1, votes: 3}
 *  |  max = {id: 0, votes: 5},   obj = {id: 2, votes: 11}
 * reduced = {id: 2, votes: 11}
 */