Javascript合并和组成具有相同ID的对象

时间:2019-03-11 16:59:04

标签: javascript merge

我一直在尝试合并数据并获得以下结果:

data = [{serviceID: 22, quantite: 120, typeConviveId: 6},
       {serviceID: 23, quantite: 240, typeConviveId: 6},
        {serviceID: 24, quantite: 100, typeConviveId: 7},
        {serviceID: 25, quantite: 150, typeConviveId: 7}]  

最后需要什么:

result: [ { "22": "120", "23": "240", "typeConviveId": "6"},
        { "24": "100", "25": "150", "typeConviveId": "7" } ] 

1 个答案:

答案 0 :(得分:0)

您可以使用Array.prototype.mapArray.prototype.filterArray.prototype.reduce的组合,以及使用typeConviveId和{{1 }}:

Set