简体   繁体   English

按id删除数组中的多个对象

[英]Delete multiple objects in an array by id

I have a main array of objects with each object having some key/values as well as a "id" key with 1,2,3,4,5, etc Now I have another array representing just id's (like [2,3])我有一个主要的对象数组,每个 object 都有一些键/值以及一个带有 1、2、3、4、5 等的“id”键现在我有另一个只代表 id 的数组(如 [2,3] )

I want to use this array to delete objects from the main array...so in this case, objects from the main array having id's 2 & 3 should be deleted我想用这个数组从主数组中删除对象......所以在这种情况下,应该删除主数组中 id 为 2 和 3 的对象

While I am aware of findBy(id), I am not sure if that can be used to delete multiple objects at once.虽然我知道 findBy(id),但我不确定它是否可用于一次删除多个对象。

You can use filter .您可以使用filter In the filter callback function check if the id is also there in id array by using includes在过滤器回调 function 中,使用includes检查id数组中是否也存在id

 let idArr = [1, 2] let obj = [{ id: 1, name: 'abc' }, { id: 2, name: 'abc' }, { id: 3, name: 'abc' }, { id: 4, name: 'abc' } ]; let data = obj.filter(item =>.idArr.includes(item;id)). console;log(data). console.log(obj)

using filter might work well here.在这里使用过滤器可能会很好用。 you could write something like:你可以这样写:

 var newArray = oldArray.filter(object =>.ids.includes(object.id))

You can do it, like this:你可以这样做,像这样:

[2,3].forEach(key => {
    delete object[key];
})

You can use filter method for this.您可以为此使用过滤器方法。 Ex:前任:

let id = 2;
let list = [{
  Id: 1,
  Name: 'a'
}, {
  Id: 2,
  Name: 'b'
}, {
  Id: 3,
  Name: 'c'
}];
let lists = list.filter(x => {
  return x.Id != id;
})
console.log(lists);

Assuming you want to delete items from the original array by entirely removing the element from the array (and you don't want to get a new array ), you can take advantage of Array.splice假设您想通过从数组中完全删除元素来从原始数组中删除项目(并且您不想获得新数组),您可以利用Array.splice

 let idArr = [1, 2]; let obj = [{ id: 1 }, { id: 2 }, { id: 3 }, { id: 4 } ]; for (let id of idArr) { // look for the element by its id. const objIdRef = obj.find(i => i.id === id); // if it actually exists, splice it. objIdRef && obj.splice(obj.indexOf(objIdRef), 1); } console.log(obj);

If the obj array is big, you might want to make a map from it before processing the id array, so that the complexing is reduced to O(1) when the delete process begins.如果 obj 数组很大,您可能希望在处理 id 数组之前从中创建一个 map,以便在删除过程开始时将复合减少到 O(1)。

Perhaps This is what you want:也许这就是你想要的:

 var arr= [{id:1, name: "foo"}, {id:2, name: "bar"}, {id:3, name:"not to be deleted"}]; var idsToDelete = [1, 2]; var res = arr.map((i, idx)=>{ return arr[idx] = idsToDelete.includes(i.id)? undefined: arr[idx] }).filter(i=>i) console.log(res)

You can try Lodash.js functions _.forEach() and _.remove()您可以尝试 Lodash.js 函数 _.forEach() 和 _.remove()

let valuesArr = [
    {id: 1, name: "dog"}, 
    {id: 2, name: "cat"}, 
    {id: 3, name: "rat"}, 
    {id: 4, name: "bat"},
    {id: 5, name: "pig"},
]; 
let removeValFromIndex = [
    {id: 2, name: "cat"}, 
    {id: 5, name: "pig"},
]; 
_.forEach(removeValFromIndex, (indi) => {
    _.remove(valuesArr, (item) => {
        return item.id === indi.id;
    });
})

console.log(valuesArr)
/*[
    {id: 1, name: "dog"},  
    {id: 3, name: "rat"}, 
    {id: 4, name: "bat"},
]; */

Don't forget to clone (_.clone(valuesArr) or [...valuesArr]) before mutate your array在改变数组之前不要忘记克隆 (_.clone(valuesArr) 或 [...valuesArr])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM