我有一个对象数组,我想只获取数量最多的对象,在这个例子中是'id'的对象:4,并尝试使用javascript的filter属性,但我没有实现了,不然我能实现吗?
[
{
"id": 1,
"quantity": 10,
"price": 80
},
{
"id": 2,
"quantity": 30,
"price": 170
},
{
"id": 3,
"quantity": 50,
"price": 230
},
{
"id": 4,
"quantity": 100,
"price": 100
}
]
最佳答案
在这种情况下,reduce
是正确的选择:
const most = array.reduce((a, b) => a.quantity > b.quantity ? a : b);
关于javascript - 根据其中一个键的条件,从数组中获取单个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51565051/