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
                })
            })
Philx94
  • 1,036
  • 3
  • 20
  • 39

3 Answers3

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

Ashik Paul
  • 486
  • 4
  • 20
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)
Sven.hig
  • 4,449
  • 2
  • 8
  • 18
  • 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 – Sven.hig Jul 08 '20 at 19:15
  • 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 – Sven.hig Jul 08 '20 at 19:18
  • When I test this, I'm only getting this as a result: 0: Object { x: 1, y: 1 } – baumli Jul 08 '20 at 19:23
  • 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 – Sven.hig Jul 08 '20 at 19:26
  • @baumli You can test the code with the data provided above and it works as well – Sven.hig Jul 08 '20 at 19:27
  • So you should have payed more attention and **read** the code very well before downvoting an answer – Sven.hig Jul 08 '20 at 19:28
  • I didn't downvote, was just testing the code. I see that with your arrays it does indeed function as it should. I suspect that the downvote may be the result of them not seeing the arrays were different as well. Upvoted. – baumli Jul 08 '20 at 19:35
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>
baumli
  • 421
  • 5
  • 24