Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular5 compare 2 array of (JSON) objects and splice when found match

If i have 2 arrays with (JSON) objects and i want to compare them and splice an object when there is a match what is the best way to do this.

Example:

Array 1:

[{test: 1, test2: 2}, {test: 3, test2: 5}, {test: 6, test2: 8}]

Array 2:

[{test: 6, test2: 8}, {test: 1, test2: 2}]

Now we see that Array 1 index 0 and Array 2 index 1 are a match. What I want to do now is splice the object from array 1.

Is there a good/smart way to achieve this?

thx!

like image 867
Hans Avatar asked Jun 19 '26 21:06

Hans


1 Answers

try this, using filter, filter will loop over array1 and return an array of elements that match the condition (element that doesn't exist in array2)

array2.foreach((element) => {
 array1 = array1.filter((element1) => {
   return element1 !== element;
 })
})
like image 188
Fateh Mohamed Avatar answered Jun 21 '26 16:06

Fateh Mohamed



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!