やりたいこと
[
{
email: 'b@gmail.com',
tags: ['VIP', 'male'],
},
{
email: 'a@gmail.com',
tags: ['VIP', 'male', 'cool'],
},
{
email: 'c@gmail.com',
tags: ['VIP', 'female'],
},
{
email: 'd@gmail.com',
tags: ['female'],
},
];
これを
['female']
と比較して、femaleが入っている要素だけ取得したい
↓
[
{
email: 'c@gmail.com',
tags: ['VIP', 'female'],
},
{
email: 'd@gmail.com',
tags: ['female'],
},
];
解決方法
const customers = [
{
email: 'b@gmail.com',
tags: ['VIP', 'male'],
},
{
email: 'a@gmail.com',
tags: ['VIP', 'male', 'cool'],
},
{
email: 'c@gmail.com',
tags: ['VIP', 'female'],
},
{
email: 'd@gmail.com',
tags: ['female'],
},
];
const compareData = ['female', 'VIP'];
const result = customers.filter((el) =>
el.tags.some((r) => compareData.includes(r))
);
console.log(result);