如何合并对象并只对重复对象的某些值求和?

时间:2017-08-17 13:04:17

标签: javascript arrays reactjs sorting lodash

假设我有一个对象数组:

[
    1: {name: "john", id: 200, points: "333"},
    2: {name: "alice", id: 302, points: "20"},
    3: {name: "alice", id: 302, points: "13"},
    4: {name: "kevin", id: 501, points: "3"},
    5: {name: "adam", id: 999, points: "100"},
    6: {name: "adam", id: 999, points: "30"},
    7: {name: "adam", id: 999, points: "60"},
]

如您所见,它们已经排序(使用lodash)。 我想要实现的是我想合并具有相同id的对象并添加点。所以它看起来像这样:

[
    1: {name: "john", id: 200, points: "333"},
    2: {name: "alice", id: 302, points: "30"},
    3: {name: "kevin", id: 501, points: "3"},
    4: {name: "adam", id: 999, points: "190"},
]

我正在使用React和Lodash。该阵列中有大约7000条记录。

1 个答案:

答案 0 :(得分:0)

没有Lodash的解决方案

semanage fcontext -at httpd_sys_content_t '/etc/php.ini'
semanage fcontext -at httpd_sys_content_t '/etc/php.d/(/.*)?'
restorecon -Rv /etc/
systemctl restart httpd

https://jsbin.com/dififukiqa/edit?js,console