我有两个数组,一个是原始数组,第二个是原始数组的副本。我将一些新项目放入数组中,并更新其中一些,并希望将其与复制数组进行比较,并希望消除复制数组中的那些项目,并将这些项目保留在Id = null
var original = [
{
"Id": 1,
"BrandConstruct": 265,
"YearPlanData": "a"
},
{ "Id": 2,
"BrandConstruct": 236,
"YearPlanData": "c"
},
{ "Id": 3,
"BrandConstruct": 376,
"YearPlanData": "b"
},
{ "Id": null,
"BrandConstruct": 476,
"YearPlanData": "e"
},
{ "Id": null,
"BrandConstruct": 576,
"YearPlanData": "f"
}
]
var copy = [
{
"Id": 1,
"BrandConstruct": 165,
"YearPlanData": "a"
},
{ "Id": 2,
"BrandConstruct": 236,
"YearPlanData": "c"
},
{ "Id": 3,
"BrandConstruct": 376,
"YearPlanData": "b"
}
]
这是两个具有属性ID,BrandConstruct和YearPlanData的数组使用复制数组,但将数据保留在Id = null的地方 所以我想在比较后得到这种类型的结果,它必须在IE上可以工作
var original = [
{
"Id": 1,
"BrandConstruct": 265,
"YearPlanData": "a"
},
{ "Id": null,
"BrandConstruct": 476,
"YearPlanData": "e"
},
{ "Id": null,
"BrandConstruct": 576,
"YearPlanData": "f"
}
]
答案 0 :(得分:5)
const newOriginal = original.filter(obj => {
return !copy.find(copyObj => JSON.stringify(copyObj) === JSON.stringify(obj))
});
这应该可以在Internet Explorer上运行:
const newOriginal = original.filter(function(obj) {
return !copy.filter(function(copyObj) { return JSON.stringify(copyObj) === JSON.stringify(obj)}).length
});
答案 1 :(得分:1)
您可以遍历original
数组,并将不属于copy
数组的元素推到新数组。
function isInArray(item, array) {
return JSON.stringify(array).indexOf(JSON.stringify(item)) > -1
}
function getFilteredArray(original, copy) {
var newOriginal = [];
for (var i = 0; i < original.length; i++) {
var item = original[i];
if (!isInArray(item, copy) || item.id === null) newOriginal.push(item);
}
return newOriginal;
}
var original = [{
"Id": 1,
"BrandConstruct": 265,
"YearPlanData": "a"
},
{
"Id": 2,
"BrandConstruct": 236,
"YearPlanData": "c"
},
{
"Id": 3,
"BrandConstruct": 376,
"YearPlanData": "b"
},
{
"Id": null,
"BrandConstruct": 476,
"YearPlanData": "e"
},
{
"Id": null,
"BrandConstruct": 576,
"YearPlanData": "f"
}
]
var copy = [{
"Id": 1,
"BrandConstruct": 165,
"YearPlanData": "a"
},
{
"Id": 2,
"BrandConstruct": 236,
"YearPlanData": "c"
},
{
"Id": 3,
"BrandConstruct": 376,
"YearPlanData": "b"
}
]
console.log(getFilteredArray(original, copy));
答案 2 :(得分:1)
这是为了处理对象,您可以在找不到要搜索的属性的索引时将它们推入一个空数组,从而创建一个唯一值的新数组:
for (var key in array_of_objects) {
var index = empty_array.findIndex(x => x.attribute == array_of_objects.attribute)
if (index === -1){
empty_array.push(d);
}
}
答案 3 :(得分:1)
这可行,但是我认为您应该等待更好的解决方案。
var original = [{
"Id": 1,
"BrandConstruct": 265,
"YearPlanData": "a"
}, {
"Id": 2,
"BrandConstruct": 236,
"YearPlanData": "c"
}, {
"Id": 3,
"BrandConstruct": 376,
"YearPlanData": "b"
}, {
"Id": null,
"BrandConstruct": 476,
"YearPlanData": "e"
}, {
"Id": null,
"BrandConstruct": 576,
"YearPlanData": "f"
}];
var copy = [{
"Id": 1,
"BrandConstruct": 165,
"YearPlanData": "a"
}, {
"Id": 2,
"BrandConstruct": 236,
"YearPlanData": "c"
}, {
"Id": 3,
"BrandConstruct": 376,
"YearPlanData": "b"
}];
copy = copy.map(function(el) {
return JSON.stringify(el);
});
original = original.map(function(el) {
return JSON.stringify(el);
}).filter(function(els) {
return copy.indexOf(els) == -1;
}).map(function(s) {
return JSON.parse(s);
});
console.log(original);