我想根据另一个数组过滤具有数组属性的对象数组:[ { id: 50, name: 'test1', countries: ['RO','GB'], }, { id: 51, name: 'test2', countries: ['DE', 'RO'], }, { id: 52, name: 'test3', countries: ['DE'], }]我想返回一个对象数组,这些对象可以通过一组国家/地区进行过滤,这意味着如果我想通过 1、2 个国家/地区进行过滤。1. countries: ['RO']或者2. countries: ['RO', 'DE'],预期输出将是:1.[ { id: 50, name: 'test1', countries: ['RO', 'DE' ,'GB'], }, { id: 51, name: 'test2', countries: ['DE', 'RO'], }]2.[ { id: 50, name: 'test1', countries: ['RO', 'DE' ,'GB'], }, { id: 51, name: 'test2', countries: ['DE', 'RO'], }, { id: 52, name: 'test3', countries: ['DE'], }]
2 回答
慕容708150
TA贡献1831条经验 获得超4个赞
您可以使用filter()和includes()方法;
var import1 = [
{
id: 50,
name: 'test1',
countries: ['RO','GB'],
}, {
id: 51,
name: 'test2',
countries: ['DE', 'RO'],
}, {
id: 52,
name: 'test3',
countries: ['DE'],
}
];
var export1 = import1.filter(function(importz){
return importz.countries.includes("DE");
});
console.log(export1);
慕娘9325324
TA贡献1783条经验 获得超4个赞
使用此函数查找javascript数组中存在的多个值
var containsAll = arr1.every(function (i) { return arr2.includes(i); });
添加回答
举报
0/150
提交
取消