Let's say I have these two arrays:
var array1 = new[] {"A", "B", "C"}; var array2 = new[] {"A", "C", "D"};
I would like to get the differences between the two. I know I could write this in just a few lines of code, but I want to make sure I'm not missing a built in language feature or a LINQ extension method.
Ideally, I would end up with the following three results:
Thanks in advance!
You can use array#map . For the first index value subtract from 0 and for other indexes, subtract from the previous number.
To get the difference between two arrays in TypeScript: Use the filter() method to iterate over the first array. Check if each element is not contained in the second array. Repeat the steps, but this time iterate over the second array.
If you've got LINQ available to you, you can use Except
and Distinct
. The sets you asked for in the question are respectively:
- array2.Except(array1) - array1.Except(array2) - array1.Intersect(array2)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With