в основном с использованием filter
и some
сделают свое дело!
const persons = [{
personId: 1,
name: 'Patrick',
lastName: 'Smith',
age: 27,
allergy: [{
idAllergy: 1,
name: 'Fish'
}, {
idAllergy: 2,
name: 'Nuts'
}]
},
{
personId: 2,
name: 'Lara',
lastName: 'Blake',
age: 21,
allergy: [{
idAllergy: 2,
name: 'Nuts'
}]
},
{
personId: 3,
name: 'Erick',
lastName: 'Robinson',
age: 30,
allergy: [{
idAllergy: 3,
name: 'Flowers'
}]
},
{
personId: 4,
name: 'Hilda',
lastName: 'Vianne',
age: 35,
allergy: [{
idAllergy: 4,
name: 'Chocolat'
}]
}
]
const allergies = [2, 3]
const resultWithAllergies = persons.filter(person => {
const {
allergy
} = person;
const hasAllergy = allergy.some(({
idAllergy
}) => allergies.includes(idAllergy))
return hasAllergy;
})
console.log(resultWithAllergies)
const persons = [{
personId: 1,
name: 'Patrick',
lastName: 'Smith',
age: 27,
allergy: [{
idAllergy: 1,
name: 'Fish'
}, {
idAllergy: 2,
name: 'Nuts'
}]
},
{
personId: 2,
name: 'Lara',
lastName: 'Blake',
age: 21,
allergy: [{
idAllergy: 2,
name: 'Nuts'
}]
},
{
personId: 3,
name: 'Erick',
lastName: 'Robinson',
age: 30,
allergy: [{
idAllergy: 3,
name: 'Flowers'
}]
},
{
personId: 4,
name: 'Hilda',
lastName: 'Vianne',
age: 35,
allergy: [{
idAllergy: 4,
name: 'Chocolat'
}]
}
]
const allergies = [2, 3];
const resultWithoutAllergies = persons.filter(person => {
const {
allergy
} = person;
const hasAllergy = allergy.some(({
idAllergy
}) => !allergies.includes(idAllergy))
return hasAllergy;
})
console.log(resultWithoutAllergies)