0

What I am trying to achieve is compare two arrays with image source strings and get the difference of that and store that in a new array.

Here is how I execute the function with two arrays as parameters

compareImgUrls(extractedResources, currentExtractedResources);

and this it the function:

function compareImgUrls(array1, array2) {

            var a = [], 
            diff = [];

            for (var i = 0; i < array1.length; i++) {
                a[array1[i]] = true;
            }

            for (var i = 0; i < array2.length; i++) {
                if (a[array2[i]]) {
                    delete a[array2[i]];
                } else {
                    a[array2[i]] = true;
                }
            }

            for (var k in a) {
                diff.push(k);
            }
            console.log('diff', diff)
            return diff;
        }

But this returns an array with all strings behind each other and not the difference. How can I compare these arrays and delete the duplicated strings, and store the others in a new array.

Sireini
  • 4,142
  • 12
  • 52
  • 91
  • 1
    `let diff = (arr1, arr2) => arr1.concat(arr2).filter((x, i, a) => a.indexOf(x !== i));` – Jared Smith Mar 15 '18 at 14:36
  • @JaredSmith didn't know this one, very nice – Logar Mar 15 '18 at 14:44
  • @JaredSmith does not work for me still returns me (2) [Array(61), Array(13)] – Sireini Mar 15 '18 at 14:46
  • 1
    @Sreinieren sorry I did union not disjunction. You want `let diff = (arr1, arr2) => { let all = arr1.concat(arr2); let dupes = all.filter((x, i, a) => a.indexOf(x) !== i); return all.filter(x => !dupes.includes(x)); };` – Jared Smith Mar 15 '18 at 14:57

0 Answers0