[英]How to filter array to know if multiChild array consists ONLY of children of a certain type?
我有一个数据结构,如:
const Items = [
{
coolitmes: [
{
coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
},
{ coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
{ coolerItems: [{ type: 'cat' }, { type: 'cat' }, { type: 'cat' }] }
]
},
{
coolitmes: [
{
coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
},
{ coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
{ coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
]
},
{
coolitmes: [
{
coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }]
},
{ coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] },
{ coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }
]
}
];
我有 3 个cat类型的对象
我试图弄清楚如何编写一个过滤算法来告诉我我的数组是否仅包含类型为 cat 的子对象。
至今:
function isAllOfType(type: string) {
for (let i = 0; i <= Items.length; i++) {
for (let i2 = 0; i2 <= Items[i].coolitmes.length; i2++) {
for (let i3 = 0; i3 <= Items[i].coolitmes[i2].coolerItems.length; i3++) {
if (Items[i].coolitmes[i2].coolerItems[i3].type === type) {
// what do I do here
}
}
}
}
// return true/false depending if all chidlren are of certian type
}
const isAllCat = isAllOfType('cat')
我不确定我的想法是否正确。 我需要知道狗类型的总数,然后是猫类型的总数,如果没有狗类型而只有猫类型,那么我知道。 我该如何做到这一点?
真的,这只是归结为 1 行:
Items.flatMap(x => x.coolitmes).flatMap(x => x.coolerItems).every(x => x.type == type);
这可以包装在 function 中,如下所示:
// Mostly dogs, but some cats const Items = [ { coolitmes: [ { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'cat' }, { type: 'cat' }, { type: 'cat' }] } ] }, { coolitmes: [ { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] } ] }, { coolitmes: [ { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] } ] } ]; // All dogs const Items2 = [ { coolitmes: [ { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] } ] }, { coolitmes: [ { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] } ] }, { coolitmes: [ { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] }, { coolerItems: [{ type: 'dog' }, { type: 'dog' }, { type: 'dog' }] } ] } ]; function isAllOfType(items, type){ return items.flatMap(x => x.coolitmes).flatMap(x => x.coolerItems).every(x => x.type == type); } console.log(isAllOfType(Items,"cat")); console.log(isAllOfType(Items2,"dog"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.