我试图删除数组的对象,如果它的ID与其他数组中的对象相同。但我不知道该怎么做。
示例:
parentArray1 = [
{
id: 101
...
}, {
REMOVE THIS OBJECT BECAUSE ITS ID IS THE SAME AS parentArray2[0].id
id: 102
...
}, {
id: 103
...
}
];
parentArray2 = [
{
id: 102 // SAME ID AS parentArray1[1].id
...
}
];
有没有人有想法或解决方案?
答案 0 :(得分:4)
过滤,如果元素与第二个某些元素不匹配,则保留该元素:
parentArray1.filter(elt1 => !parentArray2.some(elt2 => elt2.id === elt1.id))
这是逐行评论的格式:
parentArray1 // From parentArray1
.filter( // keep all the
elt1 => // elements for which
! // it is not the case that
parentArray2 // parentArray2
.some( // has some
elt2 => // elements for which
elt2.id // the id
=== // is equal to
elt1.id)) // the id of the first element.
为您进行一些ES6解构:
parentArray1.filter(({id1}) => !parentArray2.some(({id2}) => id1 === id2))
在ES5中,没有箭头功能:
parentArray.filter(function(elt1) {
return !parentArray2.some(function(elt2) {
return elt2.id === elt1.id;
});
})
如果你想用for循环执行此操作:
result = [];
for (var i = 0; i < parentArray1.length; i++) {
var include = true;
for (var j = 0; j < parentArray2.length; j++) {
if (parentArray1[i].id === parentArray2[j].id) include = false;
}
if (include) result.push(parentArray1[i]);
}
您可能还想考虑预先计算要删除的ID列表:
var removeIds = parentArray2.map(elt => elt.id);
现在您可以更轻松地过滤parentArray1
:
parentArray1.filter(elt => !removeIds.contains(elt.id))
答案 1 :(得分:1)
您可以使用filter()
和find()
来获得所需的结果。
var parentArray1 = [{
id: 101
}, {
id: 102
}, {
id: 103
}];
var parentArray2 = [{
id: 102
}, {
id: 199
}];
var result = parentArray1.filter(function(o) {
return !parentArray2.find(function(e) {
return o.id == e.id
})
})
console.log(result)
答案 2 :(得分:1)
好吧,我准备了一些示例代码(复制到第3个数组,从数组中删除,function
和prototype
)。
试试:
复制到新阵列:
var a = [
{id:1,name:"Paul"},
{id:2,name:"Andre"},
{id:3,name:"Sophie"},
{id:4,name:"Anton"},
{id:5,name:"John"},
{id:6,name:"Einar"}
];
var b = [
{id:2,name:"Andre"},
{id:5,name:"John"}
];
var c = [];
a.forEach(function(aItem, aIndex) {
var copy = true;
b.forEach(function(bItem, bIndex) {
if (aItem.id === bItem.id) {
copy = false;
}
});
if(copy === true) {
c.push(aItem);
}
});
console.log(c);
从第一个阵列中删除:
var a = [
{id:1,name:"Paul"},
{id:2,name:"Andre"},
{id:3,name:"Sophie"},
{id:4,name:"Anton"},
{id:5,name:"John"},
{id:6,name:"Einar"}
];
var b = [
{id:2,name:"Andre"},
{id:5,name:"John"}
];
a.forEach(function(aItem, aIndex) {
b.forEach(function(bItem, bIndex) {
if (aItem.id === bItem.id) {
a.splice(aIndex, 1);
}
});
});
console.log(a);
作为功能:
var a = [
{id:1,name:"Paul"},
{id:2,name:"Andre"},
{id:3,name:"Sophie"},
{id:4,name:"Anton"},
{id:5,name:"John"},
{id:6,name:"Einar"}
];
var b = [
{id:2,name:"Andre"},
{id:5,name:"John"}
];
function removeItemFromArrayByIndexObject(from, to, index) {
var result = [];
for (var i = 0; i < from.length; i++) {
var append = true;
for (var j = 0; j < to.length; j++) if (from[i][index] === to[j][index]) append = false;
if (append === true) result.push(from[i]);
}
return result;
}
console.log(removeItemFromArrayByIndexObject(a, b, 'id'))
原型:
// first array
var a = [
{id:1,name:"Paul"},
{id:2,name:"Andre"},
{id:3,name:"Sophie"},
{id:4,name:"Anton"},
{id:5,name:"John"},
{id:6,name:"Einar"}
];
// second array
var b = [
{id:2,name:"Andre"},
{id:5,name:"John"}
];
// create prototype
Array.prototype.removeItemFromArrayByIndexObject = function (to, index) {
var result = [];
for (var i = 0; i < this.length; i++) {
var remove = false;
for (var j = 0; j < to.length; j++) if (this[i][index] === to[j][index]) remove = true;
if (remove === true) this.splice(i, 1);
}
return this;
}
// apply
a.removeItemFromArrayByIndexObject(b, 'id')
// log
console.log(a);
答案 3 :(得分:1)
您可以使用findIndex
,如果您获得有效索引,请将其拼接。
parentArray1 = [{
id: 101
}, {
//REMOVE THIS OBJECT BECAUSE ITS ID IS THE SAME AS parentArray2[0].id
id: 102
}, {
id: 103
}, {
//REMOVE THIS OBJECT BECAUSE ITS ID IS THE SAME AS parentArray2[0].id
id: 102
}];
parentArray2 = [{
id: 102 // SAME ID AS parentArray1[1].id
}];
for(var i = 0; i< parentArray2.length; i++){
var index = parentArray1.findIndex(x=>x.id === parentArray2[i].id);
if(index !== -1){
parentArray1.splice(index,1);
i--;
}
}
console.log(parentArray1)
&#13;