[英]Javascript - Filter object containing arrays with value from another array
I have a problem implementing filtering for nested data.我在实现嵌套数据过滤时遇到问题。 I have this kind of data from API :
我有来自 API 的这种数据:
const animals = {
bugs: ['ant', 'cricket'],
fish: ['shark', 'whale', 'tuna'],
mammals: ['cow', 'horse', 'sheep'],
birds: ['eagle', 'crow', 'parrot'],
predators: ['tiger', 'lion']
}
I have to filter them with this array : const data = ['shark', 'horse', 'cow', 'parrot']
我必须用这个数组过滤它们:
const data = ['shark', 'horse', 'cow', 'parrot']
The result I want to achieve :我想要达到的结果:
const filtered = {
fish: ['shark'],
mammals: ['cow', 'horse'],
birds: ['parrot'],
}
I have tried :我努力了 :
filter.forEach((item) => {
for (let key in animals) {
let species = []
if (animals[key].includes(item)) {
filtered[key] = [...species, species]
}
}
})
and the result :结果:
const filtered = {
fish: ['whale'],
mammals: ['cow',],
birds: ['parrot'],
}
I still can't achieve the desired outcome, because the items inside array will not be added but replaced.我仍然无法达到预期的结果,因为数组中的项目不会被添加而是被替换。 I'm stuck here.
我被困在这里。 Any help will be really appreciated.
任何帮助将不胜感激。 Thanks !
谢谢 !
First you need to loop your object then filter arrays of animals according to data.首先,您需要循环您的对象,然后根据数据过滤动物数组。
const animals = { fish: ['shark', 'whale', 'tuna'], mammals: ['cow', 'horse', 'sheep'], birds: ['eagle', 'crow', 'parrot'],}; const data = ['shark', 'horse', 'cow', 'parrot']; let filtered = {}; for (var a of Object.keys(animals)) { filtered[a] = animals[a].filter(value => data.includes(value)); } console.log(filtered);
You could rebuild the entries of the object.您可以重建对象的条目。
const animals = { bugs: ['ant', 'cricket'], fish: ['shark', 'whale', 'tuna'], mammals: ['cow', 'horse', 'sheep'], birds: ['eagle', 'crow', 'parrot'], predators: ['tiger', 'lion'] }, data = ['shark', 'horse', 'cow', 'parrot'], result = Object.fromEntries(Object .entries(animals) .flatMap(([k, a]) => { a = a.filter(v => data.includes(v)); return a.length ? [[k, a]] : [] }) ); console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
You can use the filter
method:您可以使用
filter
方法:
const fish = animals.fish.filter((animal) => animal === 'shark');
const mammals = animals.mammals.filter((animal) => animal === 'cow' || animal === 'horse');
const birds = animals.birds.filter((animal) => animal === 'parrot');
const filtered = {
fish, // equals to fish: fish
mammals, // equals to mammals: mammals
birds, // equals to birds: birds
};
Note that this is just an example.请注意,这只是一个示例。 You can put your own checks in the callback functions but it should return a boolean.
您可以将自己的检查放在回调函数中,但它应该返回一个布尔值。
You can iterate over the object and create a new one with the filtered data您可以迭代对象并使用过滤后的数据创建一个新对象
const animals = { bugs: ['ant', 'cricket'], fish: ['shark', 'whale', 'tuna'], mammals: ['cow', 'horse', 'sheep'], birds: ['eagle', 'crow', 'parrot'], predators: ['tiger', 'lion'] } const data = ['shark', 'horse', 'cow', 'parrot'] const filtered = {}; for ( arr in animals) { filtered[arr] = animals[arr].filter ( x => data.includes(x)) } console.log(filtered)
EDIT if you don't want empty arrays as part of the solution:如果您不希望将空数组作为解决方案的一部分,请编辑:
const animals = { bugs: ['ant', 'cricket'], fish: ['shark', 'whale', 'tuna'], mammals: ['cow', 'horse', 'sheep'], birds: ['eagle', 'crow', 'parrot'], predators: ['tiger', 'lion'] } const data = ['shark', 'horse', 'cow', 'parrot'] const filtered = {}; let tmp = []; for ( arr in animals) { tmp = animals[arr].filter ( x => data.includes(x)) if (tmp.length > 0) filtered[arr] = tmp } console.log(filtered)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.