将一组对象存储在数组中。如果我想比较像重量这样的属性,我将如何以最有效的方式来做到这一点?比方说,我想水果篮是全时体重= 10比较数组中的对象属性
var fruitBasket = []
function addFruit(fruit, weight) {
return {
fruit: fruit,
weight: weight
}
}
fruitBasket.push(addFruit(apple, 2));
fruitBasket.push(addFruit(orange, 3));
fruitBasket.push(addFruit(watermelon, 5));
//etc...
使用'Array.prototype.reduce'与功能,增加了'重量'属性。 – Barmar