gpt4 book ai didi

javascript - 如果两个对象数组中对象的属性值相等,则将这些对象合并为单个对象

转载 作者:行者123 更新时间:2023-12-03 01:35:50 25 4
gpt4 key购买 nike

如果 x 属性值在 2 个数组中的两个对象中都匹配。将这些对象合并为单个对象。

var data3= [{"x":"Mfg1","y":83840.901245},{"x":"Mfg2","y":74804.973325},{"x":"Mfg3","y":81869.98221666666},{"x":"Mfg4","y":36503.152352499994}];
var data4 = [{"x":"Mfg1","y":83840.901245},{"x":"Mfg2","y":74804.973325},{"x":"Mfg8","y":51869.98221666666},{"x":"Mfg4","y":36503.152352499994}];

输出数组应该是:

[{"x":"Mfg1","y":83840.901245},{"x":"Mfg2","y":74804.973325},{"x":"Mfg3","y":81869.98221666666},{"x":"Mfg4","y":36503.152352499994},{"x":"Mfg8","y":51869.98221666666}]

最佳答案

您可以使用对象的字符串化版本来检查重复项:

const unique = (getKey, s = new Set) => el => !s.has(getKey(el)) && s.add(getKey(el));

const result = data3.concat(data4).filter( unique(el => JSON.stringify(el)) );

关于javascript - 如果两个对象数组中对象的属性值相等,则将这些对象合并为单个对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51080842/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com