按值数组es6过滤对象数组

时间:2020-02-27 15:39:03

标签: javascript typescript ecmascript-6

我需要帮助来完成一个函数,该函数通过具有值的数组来过滤对象数组,例如:

我的对象数组:

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

]

我的数组具有要过滤的值:

//这些是idAllergy 让过敏= [2,3]

所以该计划是使用过敏数组值对那些对坚果和花过敏的人而不是向他们展示过敏的人,所以我的预期结果将是:

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

预先感谢

3 个答案:

答案 0 :(得分:1)

如果您希望对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);

答案 1 :(得分:1)

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);

答案 2 :(得分:1)

基本上使用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)