FeR*_*cHo 1 javascript arrays javascript-objects ecmascript-6
我有一个对象数组,我想只获取具有最大数量的对象,在本例中是'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
}
]
Run Code Online (Sandbox Code Playgroud)
在这种情况下,reduce是正确的选择:
const most = array.reduce((a, b) => a.quantity > b.quantity ? a : b);
Run Code Online (Sandbox Code Playgroud)