简体   繁体   English

按值数组过滤对象数组 es6

[英]filter array of objects by array of values es6

i need a help to do a function to filter an array of object by an a array with values, example:我需要一个帮助来执行一个函数来通过一个带有值的数组来过滤一个对象数组,例如:

my array with objects:我的对象数组:

 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' } ] } ]

my array with values to filter:我的数组带有要过滤的值:

// these are idAllergy let allergy = [2,3] // 这些是 idAllergy let allergy = [2,3]

so the plan is use allergy array values to seach into persons for people who has allergy to nuts and flowers and dont show them so my expected result will be:所以计划是使用过敏数组值来搜索对坚果和鲜花过敏的人,而不是向他们展示,所以我的预期结果将是:

 [{
    personId: 4,
    name: 'Hilda',
    lastName: 'Vianne',
    age: 35,
    allergy: [
    {
      idAllergy: 4,
      name: 'Chocolat'
    }
    ]
  }]

thanks in advance提前致谢

If you want people with the allergy 2 and 3 do this:如果您希望过敏 2 和 3 的人这样做:

let allergy = [2,3];
const personsWithAllergy = persons.filter(
   p => p.allergy.some(al => allergy.includes(al.idAllergy)));

 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 allergy = [2,3]; const personsWithAllergy = persons.filter(p => p.allergy.some(al => allergy.includes(al.idAllergy))); console.log(personsWithAllergy);

const filtered = persons.filter(p => !p.allergy.some(a => allergy.includes(a.idAllergy)));

 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' } ] } ] let allergy = [2,3] const filtered = persons.filter(p => !p.allergy.some(a => allergy.includes(a.idAllergy))); console.log(filtered);

basically using filter and some will do the trick!基本上使用filtersome可以解决问题!

 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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM