Snickbrack Snickbrack - 4 months ago 19
Javascript Question

Get Differences in arrays

I am using two divs in HTML in which I have multiple spans. Im providing a delete button to the user with which he can delete some spans (which he has to select before). And now want to determine which span-elements have been deleted.

How can I compare two arrays which are holding Selector-Results?

Is there a possibility to use something like the IEqualityComparer in C#?


Bellow a jquery solution

var firstArray = $("span.odd");
var secondArray = $("span.even");

var compareResult = $(firstArray).not(secondArray).length === 0 && $(secondArray).not(firstArray).length === 0;

console.log("Collections are equal:",compareResult);

var collectionDiff = secondArray.filter(function(){
  return secondArray.filter(this).length

<script src=""></script>
    <span class="odd">span 1</span>
    <span class="even">span 2</span>
    <span class="odd">span 3</span>
    <span class="even">span 4</span>
    <span class="odd">span 5</span>
    <span class="even">span 6</span>
    <span class="odd">span 7</span>
    <span class="even">span 8</span>
    <span class="odd">span 9</span>
    <span class="even">span 10</span>