Matt
Matt

Reputation: 1119

Isolate value that is not contained in one array from another array

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

Upvotes: 0

Views: 42

Answers (1)

Anastazy
Anastazy

Reputation: 4766

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)

Upvotes: 1

Related Questions