3

Given,

someArray = [{name:"Kristian", lines:"2,5,10"},
             {name:"John", lines:"1,19,26,96"},
             {name:"Brian",lines:"3,9,62,36" }];

removeArray = [{name:"Kristian", lines:"2,5,10"},
             {name:"Brian",lines:"3,9,62,36" }];

How do I remove objects of removeArray from someArray? I can remove a single object:

johnRemoved = someArray.filter(function(el) {
return el.name !== "John";
});

However, instead of comparing someArray names to a string, I'd like to compare them to names in removeArray. Can it be done with a second filter method or does it have to be a for loop?

6 Answers 6

4

You just need a second some iteration:

johnRemoved = someArray.filter( obj => !removeArray.some( obj2 => obj.name === obj2.name ));
Sign up to request clarification or add additional context in comments.

Comments

2

You could use filter with the this object equal to the set of names that need removal (a Set for efficiency):

someArray = [{name:"Kristian", lines:"2,5,10"},
             {name:"John", lines:"1,19,26,96"},
             {name:"Brian",lines:"3,9,62,36" }];

removeArray = [{name:"Kristian", lines:"2,5,10"},
             {name:"Brian",lines:"3,9,62,36" }];
             
someArray = someArray.filter(function(obj) {
    return !this.has(obj.name);
}, new Set(removeArray.map(obj => obj.name)));

console.log(someArray);

1 Comment

Why not (set => obj => !set.has(obj))(new Set(..)) ?
2

I like the answers given here. I wanted to add my own as well.

1 - Two Array.prototype.filter() methods, first filter used for iteration:

removeArray.filter(function(ra) {
    someArray = someArray.filter(function(sa) {
        return sa.name !== ra.name;
    });
});

2 - first iteration can be replaced by for...of loop

for (let item of removeArray){

3- or by Array.prototype.forEach()

removeArray.forEach(function(ra) {

4- as dubbha, Adam and Jonas w mentioned, Array.prototype.some():

someArray.filter(i => !removeArray.some(j => j.name === i.name));

5- lastly trincot's answer was interesting for me:

someArray = someArray.filter(function(obj) {
  return !this.has(obj.name);
}, new Set(removeArray.map(obj => obj.name)));

Comments

1

Filter and some:

someArray.filter(function(item) {
    return !removeArray.some(function(r) { return r.name == item.name && r.lines == item.lines })
});

Comments

1
someArray.filter(i => !removeArray.map(j => j.name).includes(i.name));

or if you don't want to go beyond ES6 with includes:

someArray.filter(i => !removeArray.some(j => j.name === i.name));

or using reduce:

someArray.reduce((acc, i) => {
  !removeArray.some(j => j.name === i.name) && acc.push(i);
  return acc;
}, []);

Comments

0

That should do the trick.

const removed = someArray.filter((e) => {
    return removeArray.find(r => r.name === e.name) !== undefined;
});

3 Comments

why const?, and not var or let
You can use whatever you want. I am primarily using const because it prevents you from re-assigning new value to variable. Here you've got pretty nice article explaining the difference between those keywords.
In this case to work with variables of our application we change them frequently, it is better to use var or` let. const more I would use it for configuration variables for example ...

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.