1

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])

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

While I am aware of findBy(id), I am not sure if that can be used to delete multiple objects at once.

4

7 Answers 7

10

You can use filter. In the filter callback function check if the id is also there in id array by using includes

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)

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks...So, it affects the original array...Correct
@testndtv no it does not affect main array
What if we want to force that...i.e. the original array to be affected...
2

using filter might work well here. you could write something like:

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

Comments

0

You can do it, like this:

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

1 Comment

This removes by index, not by id.
0

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);

Comments

0

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

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.

Comments

0

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)

Comments

0

You can try Lodash.js functions _.forEach() and _.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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.