2

I know similar questions have been posted but they never seems to target the same problem.

I want to remove Objects contained in the second array (itemsToRemove) from the first array (allItems).

allItems = [{x:1, y:2}, {x:1, y:1}, {x:4, y:1}]

itemsToRemove = [{x:1, y:2}]

result = [{x:1, y:1}, {x:4, y:1}]

I've tried many ways, but it somehow fails at the find() condition

      const result = allItems.filter((itemFromAllItems ) => {
                return !itemsToRemove.find( itemToRemove => {
                    return itemFromAllItems.x === itemToRemove.x && itemFromAllItems.y === itemToRemove.y
                })
            })
5
  • You can not compare objects. Commented Jul 8, 2020 at 18:28
  • 1
    stackoverflow.com/questions/1068834/… Commented Jul 8, 2020 at 18:29
  • i am comparing 2 objects property x and y (updated) Commented Jul 8, 2020 at 18:30
  • Well you are now with that edit Commented Jul 8, 2020 at 18:35
  • can anyone please look at my answer and downvoting it with explanation of why it is wrong !! Commented Jul 8, 2020 at 19:22

3 Answers 3

1

Assuming that your objects only have x and y values, This will work.

var allItems = [{x:1, y:2}, {x:1, y:1}, {x:4, y:1}]

var itemsToRemove = [{x:1, y:2}]

var result = allItems.filter(e => !itemsToRemove.some(s => s.x === e.x && s.y === e.y));

console.log(result);

Hope this helps

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

5 Comments

Assuming they want to code. What would be a generic solution?
Sorry, I don't know how to make this dynamic
You can just replace result with allItems if you want allItems to be changed. E.g. var allItems = allItems.filter(e => !itemsToRemove.some(s => s.x === e.x && s.y === e.y));
@Philx94 Sorry, I didn't get you. Could you please explain?
Sorry, I'm having a hard time figuring why it is not working on my end. I'll be back.
1

If you simply want to filter you can use filter and some to get the data you want however if you want to literally remove the object from the array you can use this instead

allItems = [{x:1, y:2}, {x:1, y:1}, {x:4, y:1},{x:4, y:1},{x:4, y:1}]
itemsToRemove = [{x:1, y:2},{x:4, y:1}]
 for(let i=0;i<allItems.length;i++){
   o=allItems[i]
  itemsToRemove.some(v=>{if(o.x==v.x &&o.y==v.y) allItems.splice(i,1),i--})
 }
console.log(allItems)

7 Comments

Why is the downvote!!! why??? what is wrong with this response please I am interested to know, I am addressing the person who downvoted the answer, I am open to criticism show me what is the problem with this answer
Please remember before downvoting an answer that the auther made some effort to write an answer and unless the answer is very poor and bad or out of context it makes no sense to downvote it without at least providing an explanation
When I test this, I'm only getting this as a result: 0: Object { x: 1, y: 1 }
Yes because i used a different example (data) from the one used above, i added more data to stress the fact that this solution works even for same objects
@baumli You can test the code with the data provided above and it works as well
|
0

This takes the 2 arrays, compares the values, and makes a new one with only the results that don't match.

<script>
allItems = [{x:1, y:2}, {x:1, y:1}, {x:4, y:1}];
itemsToRemove = [{x:1, y:2}]
result = [];
for(var i = 0; i < allItems.length; i++){
  for(var j = 0; j < itemsToRemove.length; j++){ 
    if(JSON.stringify(allItems[i]) !== JSON.stringify(itemsToRemove[j])){
      result.push(allItems[i]);
    }
  }
} 
console.log(result);
</script>

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.