【发布时间】:2020-05-07 17:23:55
【问题描述】:
我正在比较两个 JSON 对象并仅返回差异,而且效果很好。但我也需要一个正确或错误的答案。最终数组中是否添加了任何新项目?
假设
result1 = 文件中的现有车辆
result2 = 新车数据
文件(数组)中是否添加了任何新车辆?
newItemsAdded = ????? // Your code here
const comparer = async function (result1, result2) {
//Find values that are in result1 but not in result2
var uniqueResultOne = result1.filter(function (obj) {
return !result2.some(function (obj2) {
return (obj.title == obj2.title && obj.price==obj2.price && obj.miles==obj2.miles);
});
});
//Find values that are in result2 but not in result1
var uniqueResultTwo = result2.filter(function (obj) {
return !result1.some(function (obj2) {
return (obj.title == obj2.title && obj.price==obj2.price && obj.miles==obj2.miles);
});
});
// Combine the two arrays of unique entries
var result = uniqueResultOne.concat(uniqueResultTwo);
return result;
}
【问题讨论】:
-
这与 JSON 没有任何关系。听起来您想进行深度对象比较?
-
JSON 是一种用于数据交换的文本符号。 (More here.) 如果您正在处理 JavaScript 源代码,而不是处理 string,那么您就不是在处理 JSON。
标签: javascript arrays node.js