重构请求:尝试找到更优化的方法来过滤应用多个过滤器的对象数组
Refactoring request: Trying to find more optimal way to filter an array of objects applying several filters
正在尝试通过另一个对象数组的属性来过滤一个对象数组:
我拥有的数据
const filters = [
{
"id": "breed",
"value": "bulldog"
},
{
"id": "owner",
"value": "Kevin"
}
]
const dogs = [
{
"name": "Zephyr",
"breed": "bulldog",
"owner":"Kevin"
},
{
"name": "Dude",
"breed": "labradoodle",
"owner":"Lance"
},
{
"name": "Mark",
"breed": "yorkie",
"owner":"Rob"
},
{
"name": "Cheddar",
"breed": "bulldog",
"owner":"Kevin"
},
]
输出应该是这样的:
[
{
名称:“切达干酪”,
品种:“斗牛犬”,
所有者:“凯文”,
},
{
名称:“和风”,
品种:“斗牛犬”,
所有者:“凯文”,
},
];
但我得到的只是:
[
{
"name": "Zephyr",
"breed": "bulldog",
"owner": "Kevin"
}
]
这就是我正在做的事情:
dogs.reduce((acc, obj) => {
filters.forEach((element) => {
if (dogs.find((i) => i[element.id].includes(element.value))) {
if (!acc.some((e) => e[element.id].includes(element.value))) {
acc.push({ ...obj });
}
}
});
return acc;
}, []);
使用Array#filter
and Array#every
,获取满足过滤器的狗的列表
const
filters = [ { "id": "breed", "value": "bulldog" }, { "id": "owner", "value": "Kevin" } ],
dogs = [ { "name": "Zephyr", "breed": "bulldog", "owner":"Kevin" }, { "name": "Dude", "breed": "labradoodle", "owner":"Lance" }, { "name": "Mark", "breed": "yorkie", "owner":"Rob" }, { "name": "Cheddar", "breed": "bulldog", "owner":"Kevin" } ];
const filteredDogs = dogs.filter(dog =>
filters.every(({ id, value }) => dog[id] === value)
);
console.log(filteredDogs);
这是你的答案:
const result = dogs.filter(dog => {
return filters.every(filter => dog[filter.id] == filter.value)
})
代码片段
const filters = [
{
"id": "breed",
"value": "bulldog"
},
{
"id": "owner",
"value": "Kevin"
}
]
const dogs = [
{
"name": "Zephyr",
"breed": "bulldog",
"owner":"Kevin"
},
{
"name": "Dude",
"breed": "labradoodle",
"owner":"Lance"
},
{
"name": "Mark",
"breed": "yorkie",
"owner":"Rob"
},
{
"name": "Cheddar",
"breed": "bulldog",
"owner":"Kevin"
},
];
const result = dogs.filter((dog)=> {
return filters.every((filter)=> dog[filter.id] == filter.value)
})
console.log(result)
正在尝试通过另一个对象数组的属性来过滤一个对象数组:
我拥有的数据
const filters = [
{
"id": "breed",
"value": "bulldog"
},
{
"id": "owner",
"value": "Kevin"
}
]
const dogs = [
{
"name": "Zephyr",
"breed": "bulldog",
"owner":"Kevin"
},
{
"name": "Dude",
"breed": "labradoodle",
"owner":"Lance"
},
{
"name": "Mark",
"breed": "yorkie",
"owner":"Rob"
},
{
"name": "Cheddar",
"breed": "bulldog",
"owner":"Kevin"
},
]
输出应该是这样的:
[ { 名称:“切达干酪”, 品种:“斗牛犬”, 所有者:“凯文”, }, { 名称:“和风”, 品种:“斗牛犬”, 所有者:“凯文”, }, ];
但我得到的只是:
[
{
"name": "Zephyr",
"breed": "bulldog",
"owner": "Kevin"
}
]
这就是我正在做的事情:
dogs.reduce((acc, obj) => {
filters.forEach((element) => {
if (dogs.find((i) => i[element.id].includes(element.value))) {
if (!acc.some((e) => e[element.id].includes(element.value))) {
acc.push({ ...obj });
}
}
});
return acc;
}, []);
使用Array#filter
and Array#every
,获取满足过滤器的狗的列表
const
filters = [ { "id": "breed", "value": "bulldog" }, { "id": "owner", "value": "Kevin" } ],
dogs = [ { "name": "Zephyr", "breed": "bulldog", "owner":"Kevin" }, { "name": "Dude", "breed": "labradoodle", "owner":"Lance" }, { "name": "Mark", "breed": "yorkie", "owner":"Rob" }, { "name": "Cheddar", "breed": "bulldog", "owner":"Kevin" } ];
const filteredDogs = dogs.filter(dog =>
filters.every(({ id, value }) => dog[id] === value)
);
console.log(filteredDogs);
这是你的答案:
const result = dogs.filter(dog => {
return filters.every(filter => dog[filter.id] == filter.value)
})
代码片段
const filters = [
{
"id": "breed",
"value": "bulldog"
},
{
"id": "owner",
"value": "Kevin"
}
]
const dogs = [
{
"name": "Zephyr",
"breed": "bulldog",
"owner":"Kevin"
},
{
"name": "Dude",
"breed": "labradoodle",
"owner":"Lance"
},
{
"name": "Mark",
"breed": "yorkie",
"owner":"Rob"
},
{
"name": "Cheddar",
"breed": "bulldog",
"owner":"Kevin"
},
];
const result = dogs.filter((dog)=> {
return filters.every((filter)=> dog[filter.id] == filter.value)
})
console.log(result)