-3

I need some help finding symmetric difference of a multi dimensional array, and a simple array. The first value in each inner array of the multidimensional array cells is the index that compares to the simple array.

So

array1 = [1,4,6,7]
array2 = [[1,"more",12],[8,"some",12]]

the result should be something like:

compare(array1, array2) = //[4,6,7] // there are three differences when compared this way
compare(array2, array1) = //[8,"some",12] // there is only one difference when compared this way

I need to return an array that has both difference of array1 from array2 AND difference from array2 from array1 in the same format as the lead array.

Ideally these are not overwriting the existing arrays but creates a new with the output results. There won't be other array formats besides these two array formats. Each compare can use a different function if it helps. You don't have to use the same function for both, but if you can, great.

I tried a few permutations of loop comparisons Also solutions found here How to get the difference between two arrays of objects in JavaScript And of the simple array methods here How to get the difference between two arrays in JavaScript?

But I just am not being successful. Can someone give me a hand, and also explain their solution? Any modern tools are fine as long as its broadly cross browser compatible. All my other code sticks to ES6, so that would be ideal. If whipping out a one liner solution please explain what is going on so I can learn.

Thanks!

Update @ Dave, this made sense to me, but after it failed I started trying different filter methods and other techniques in the posts above, without much success.

let newNurkles = new Array();
        for(var i = 0; i < nurkles.length; i++){
            if(this.activeNurkles.includes(nurkles[i])){
            } else {
                newNurkles.push(nurkles[i]);// if not then push to array
            }
        }
        console.warn("Nurkles to Add" + newNurkles);
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
Calsa
  • 45
  • 4
  • 1
    What have you got so far? Might want to re-write the original problem statement so it doesn't look like it's directly from your homework assignment/interview question, though. – Dave Newton Feb 02 '21 at 01:15
  • I am long past the age of homework. let newNurkles = new Array(); for(var i = 0; i < nurkles.length; i++){ if(this.activeNurkles.includes(nurkles[i][0])){ } else { newNurkles.push(nurkles[i]);// if not then push to array } } console.warn("Nurkles to Add" + newNurkles); Seemed to make sense, but after that failed I started trying different filter and compare methods from the linked stack posts above. – Calsa Feb 02 '21 at 01:19
  • There’s no age limit of homework or coding questions :) The criteria for differencing isn’t very clear, particularly in the example shown for comparing array2 to array1 (at least I don’t quite get it yet). – Dave Newton Feb 02 '21 at 01:38
  • And what purpose does `this.` serve? Are you showing partial code and steering us to where the logical error is? ... You ask a question then steer where the problem is? But how do you know where it is since you ask the question? .... Bunch of rhetorical questions: show all or more of your code. Because what you show by itself is lacking on discerning a logical error. – GetSet Feb 02 '21 at 01:39

1 Answers1

1

This shows how to perform a disjunctive union on two arrays, one being single dimensional while the other is a multidimensional array.

The symmetry is determined by each element of the single with the first element of each sub-array in the multi. The multi will only be one level deep.

Uses: Array.prototype.map(), Array.prototype.filter()

Steps:

  1. Map over the first input array
  2. For each element, filter the second input to exclude those found in first input
  3. Limit results to only the first array returned

Notes:

  • o is the iteration of array1
  • t is iteration of array2
  • t[0] represents the match key
  • t[idx] represents the current value of the sub-array being iterated
  • Results from array2 will produce a multidimensional array

const array1 = [1, 4, 6, 7];
const  array2 = [[1, "more", 12],[8, "some", 12], [7, 3, 9], [2, 7, 5, 4], [4, 3]];

const oneToTwo = array2.map((t, idx) => array1.filter(o => t[idx] !== o))[0]

const twoToOne  = array1.map(o => array2.filter(t => o !== t[0]))[0]

console.log(oneToTwo);
console.log(twoToOne)
Randy Casburn
  • 13,840
  • 1
  • 16
  • 31
  • This will take a while to digest, I will probably comment back again with more questions but points well earned. I really don't understand how o => t[idx] !== o is understood by filter. Why would that not just be t[idx] !== o. I don't get what the o => is actually doing in this case. – Calsa Feb 02 '21 at 02:47
  • It is an [arrow function](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions). It is a substitute (plus other details) for `function(o){ ... }` – Randy Casburn Feb 02 '21 at 02:57