寻找更实用的“功能”方式......
我有一个产品的对象看起来像这样(注意重复skuid
的)
"products": [ { "skuid": "B1418", "name": "Test Product 1", "price": 7, "lastOrderedDate": 20181114, "quantity": 2 },{ "skuid": "B3446", "name": "Test Product 2", "price": 6, "lastOrderedDate": 20190114, "quantity": 2 },{ "skuid": "B1418", "name": "Test Product 1", "price": 7, "lastOrderedDate": 20180516, "quantity": 5 },{ "skuid": "B3446", "name": "Test Product 2", "price": 6, "lastOrderedDate": 20180411, "quantity": 11 }]
我想创建一个新的数组,每个不同的单个对象,skuid
但SUMS所有的quantity
值,并保留最新的lastOrderedDate
。
最终结果如下:
"products": [ { "skuid": "B1418", "name": "Test Product 1", "price": 7, "lastOrderedDate": 20181114, "quantity": 7 },{ "skuid": "B3446", "name": "Test Product 2", "price": 6, "lastOrderedDate": 20190114, "quantity": 13 }]
我可以用一堆forEach和if来做,但我想学习一种更简洁的方法来做到这一点。或许有一种,那么reduce
?
相关分类