javascript 比较两个对象数组并删除第二个对象数组中具有相同属性值的项目
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/17830390/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Compare two arrays of objects and remove items in the second one that have the same property value
提问by raben
All I need to do is compare two arrays of objects and remove items in the second one that have the same property value. For example:
我需要做的就是比较两个对象数组并删除第二个对象中具有相同属性值的项目。例如:
var a = [{'name':'bob', 'age':22}, {'name':'alice', 'age':12}, {'name':'mike', 'age':13}];
var b = [{'name':'bob', 'age':62}, {'name':'kevin', 'age':32}, {'name':'alice', 'age':32}];
function remove_duplicates(a, b) {
for (var i = 0, len = a.length; i < len; i++) {
for (var j = 0, len = b.length; j < len; j++) {
if (a[i].name == b[j].name) {
b.splice(j, 1);
}
}
}
console.log(a);
console.log(b);
}
console.log(a);
console.log(b);
remove_duplicates(a,b);
I cannot understand why this does not work and instead gives:
我不明白为什么这不起作用,而是给出:
Uncaught TypeError: Cannot read property 'name' of undefined
What I expected was the following content in b:
我期望的是 b 中的以下内容:
[{'name':'kevin', 'age':32}];
回答by Snippet
回答by AlwaysALearner
You just need to breakthe inner loop when a match is found:
您只需要在找到匹配项时中断内部循环:
if (a[i].name == b[j].name) {
b.splice(j, 1);
break;
}
回答by Sirko
Your problem is, that splice()
will change the length of the array, so that your precalculated len
value will be too large and the inside the loop you try to access undefined elements.
您的问题是,这splice()
会改变数组的长度,因此您预先计算的len
值将太大,并且您尝试访问未定义元素的循环内部。
A possible solution would be to use the filter()
method:
一个可能的解决方案是使用该filter()
方法:
function remove_duplicates(a, b) {
b = b.filter( function( item ) {
for( var i=0, len=a.length; i<len; i++ ){
if( a[i].name == item.name ) {
return false;
}
}
return true;
});
console.log(a);
console.log(b);
}
回答by Geole
Instead of using two loops you might also use the findIndex function:
除了使用两个循环,您还可以使用 findIndex 函数:
for (var i = 0, len = a.length; i < len; i++) {
var ItemIndex = b.findIndex(b => b.name === a[i].name);
a.splice(ItemIndex, 1)
}
Or if you want to go completely without using a loop you might use the forEach function
或者,如果您想完全不使用循环,则可以使用 forEach 函数
a.forEach(function(item, index, array) {
var ItemIndex = b.findIndex(b => b.name === item.name);
a.splice(ItemIndex, 1)
}
回答by Narendran
compare and remove in array of object.Typically array of object data type may be typeOf is object.So that we need to convert into JSON stringify and then check condition..
在对象数组中进行比较和删除。通常对象数据类型的数组可能是typeOf is object。所以我们需要转换为JSON stringify 然后检查条件..
for(var i=0; i < a.length; i++) {
for(var j=0; j < b.length; j++) {
if(JSON.stringify(a[i]) == JSON.stringify(b[j])) {
a.splice(i, 1);
}
}
}
回答by Dhaval Marthak
Try this:
试试这个:
You are starting loop from the 0
.
您正在从0
.
for (var i = 0, len = a.length; i < len; i++) {
for (var j = 0, len = b.length; j < len-1; j++) {
if (a[i].name == b[j].name) {
b.splice(j, 1);
}
}
}
回答by Phong Vo
The root cause is that you directly splice items from array b while you are in the for loop and pre condition is a and b have same number of items.
根本原因是您在 for 循环中直接从数组 b 拼接项目,并且前提条件是 a 和 b 具有相同数量的项目。
回答by rahulxyz
let A = [
{name: 'a', age: 20},
{name: 'b', age: 30},
{name: 'c', age: 10},
]
let B = [
{name: 'a', age: 20},
{name: 'b', age: 40},
{name: 'd', age: 10},
{name: 'e', age: 20},
{name: 'f', age: 10},
]
const compareName = (obj1, obj2)=>{
return (obj1.name === obj2.name);
}
const compareAll = (obj1, obj2)=>{
return (obj1.name === obj2.name && obj1.age=== obj2.age);
}
let output = B.filter(b=>{
let indexFound = A.findIndex(a => compareName(a, b));
return indexFound == -1;
})
Depending on which Objects you want to remove use:
根据您要删除的对象使用:
- compareName : Remove objects which have common name
- compareAll : Remove objects which have both name & age common
- compareName :删除具有通用名称的对象
- compareAll :删除具有共同名称和年龄的对象
Also to find common Objects list just add use return index != -1
还要找到常见的对象列表,只需添加使用 return index != -1
PS: Refer my Githubfor Array Data Manipulation examples in Javascript
PS:请参阅我的Github中的 Javascript 数组数据操作示例