I am struggling to filter out positions from the positions
array that are already present in the people
array.
Despite trying different combinations of _.forEach
and _.filter
, I can't seem to solve it.
console.log(position)
var test = _.filter(position, function(pos) {
_.forEach(people, function(peo) {
_.forEach(peo.position, function(peoplePos) {
if(peoplePos.value == pos.value){
return false;
}
});
});
});
console.log(test)
The main issue here is that the positions are nested within each object in the people array.
var positions = [{
val: 'CEO',
label: 'CEO XXX'
}, {
val: 'CTO',
label: 'CTO XXX'
}, {
val: 'CBO',
label: 'CBO XXX'
}, {
val: 'CLO',
label: 'CLO XXX'
}]
var people = [{
id: 'AAA',
positions: [{
val: 'CEO',
label: 'CEO XXX'
}]
},{
id: 'BBB',
positions: [{
val: 'CXO',
label: 'CXO XXX'
},{
val: 'CEO',
label: 'CEO XXX'
}]
},{
id: 'CCC',
positions: [{
val: 'CTO',
label: 'CTO XXX'
}]
}]
In this case, the desired outcome would be:
var positions = [{
val: 'CBO',
label: 'CBO XXX'
}, {
val: 'CLO',
label: 'CLO XXX'
}]
This is because CBO and CLO are not represented in any object within the people array.