如果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}]
答案 0 :(得分:1)
您可以使用对象的字符串化版本检查重复项:
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)) );