0

I'm trying to find the values in arrayOne that are not contained in each of the two groups in arrayTwo. In the scenario below I would like isolate b and d for group1 and a and b for group2.

   arrayOne = ['a','b','c','d']
    
    arrayTwo = [{
        group1:[['a', 4],['c',8]],
        group2:[['c', 7],['d',11]]
    }]

I've tried several ways up at this point but can't seem to get the order correct. Here is what I currently have:

arrayTwo[0].group1.forEach(e => {
        console.log(e)
        arrayOne.forEach(f => {
            if(e[0] != f) {
                console.log(e[0])
            }
        })
    })

Expected result

b
d
Matt
  • 967
  • 2
  • 9
  • 23
  • 1
    You should put your expected result too, that will help other supporters in answering – Nick Vu Mar 24 '22 at 15:59
  • Consider using [sets](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set) and calculate their difference. Also, 'isolate' is not the right description for this. – jarmod Mar 24 '22 at 16:07

1 Answers1

1

You could use "difference" calculation from How to get the difference between two arrays in JavaScript?

Example:

const arrayOne = ['a','b','c','d']
    
const arrayTwo = [{
    group1:[['a', 4],['c',8]],
    group2:[['c', 7],['d',11]]
}]

const resultForGroup1 = arrayOne.filter(letter => !arrayTwo[0].group1.map(keyValue => keyValue[0]).includes(letter))

document.write(resultForGroup1)
Anastazy
  • 4,624
  • 2
  • 13
  • 22