[英]How to find the count of some object property in array of objects using react and javascript?
Hi below is the array of objects.嗨,下面是对象数组。
const arr_obj = [
{
id: '1',
children: [],
type: 'TYPE1',
},
{
id: '2',
children: [
{
id: '1',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '2',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '3',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
]
type: 'TYPE2',
},
{
id: '3',
children: [
{
id: '4',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '5',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
{
id: '6',
children: [
{
//some attributes
}
],
type: 'MAIN',
},
]
type: 'TYPE2',
}
]
I have to find out the count of type: 'MAIN'.我必须找出类型的数量:'MAIN'。 these 'MAIN' will be within type: "type2"
这些“主要”将在类型内:“type2”
So the expected count is 6. The outer children array can be empty and sometimes inner children array with type: "type2" is not there at all examples like below:所以预期的计数是 6。外部子数组可以是空的,有时内部子数组的类型:“type2”根本不存在,如下所示:
children: [] //empty array
children: [
{
id: '1',
children: [],
type: 'TYPE2',
},
] //no children with type: 'TYPE2'
below is the code to handle above,下面是上面处理的代码,
const findCount = (arr_obj) => {
let count = 0;
const expectedCount = 2;
const loop = (children) => {
for (const obj of children) {
const { type, children } = obj;
if (type === 'TYPE2') {
loop(children);
} else if (type === 'MAIN') {
++count;
}
}
};
loop(children);
return count > expectedCount;
};
const output = findCount(arr_obj);
the above works fine.以上工作正常。 but it doesnt handle case when inner children is [] like below,
但是当内部孩子是 [] 时,它不处理情况,如下所示,
children: [
{
id: '1',
children: [],
type: 'TYPE2',
},
] //no children for type: 'TYPE2'
how can i handle the above data with no inner children array for children of type "TYPE2".对于“TYPE2”类型的子级,我如何处理没有内部子级数组的上述数据。 could someone help me with this.
有人可以帮我解决这个问题。 thanks.
谢谢。
how can i handle the above data with no inner children array for children of type "TYPE2".
对于“TYPE2”类型的子级,我如何处理没有内部子级数组的上述数据。
You could add a guard clause to your loop
function:您可以在
loop
function 中添加一个保护子句:
if (!children) return;
Which returns directly if there are no children.如果没有孩子,则直接返回。
Resulting in:导致:
const findCount = (arr_obj) => {
let count = 0;
const expectedCount = 2;
const loop = (children) => {
if (!children) return;
for (const obj of children) {
const { type, children } = obj;
if (type === 'TYPE2') {
loop(children);
} else if (type === 'MAIN') {
++count;
}
}
};
loop(arr_obj); // <- this should probably refer to `arr_obj`
return count > expectedCount;
};
const output = findCount(arr_obj);
For no inner children, you can check if the array of children has any elements in it or not.对于没有内部子级,您可以检查子级数组中是否有任何元素。 Something like this
像这样的东西
const loop = (children) => {
for (const obj of children) {
const { type, children } = obj;
if (type === 'TYPE2' && children.length > 0) {
loop(children);
} else if (type === 'MAIN') {
++count;
}
}
};
let count = 0;
arr_obj.?.filter(item=> item.type =="TYPE2").forEach((item)=> {
if(item.children.length > 0){
item.children.forEach((childItem)=>{
if(childItem.type == "MAIN"){
count+=1;
}
})
}
})
console.log(count);// output will be 6
I modified the example by adding another valid occurrence in the nested object.我通过在嵌套的 object 中添加另一个有效匹配项来修改示例。
The following would also work.以下也将起作用。
// count the occurences of typeX as the very next of typeY const count_TypeX_Within_TypeY = (arr, typeX, typeY) => { let count = 0; arr.forEach((item) => { // check the type in current level if (item.type === typeY) { item.children.forEach((innerItem) => { // // check the type in next level if (innerItem.type === typeX) { count += 1; } }); } // do the same recursively count += count_TypeX_Within_TypeY(item.children || [], typeX, typeY); }); return count; }; const arr_obj = [{"id":"1","children":[],"type":"TYPE1"},{"id":"2","children":[{"id":"1","children":[{}],"type":"MAIN"},{"id":"2","children":[{}],"type":"MAIN"},{"id":"3","children":[{}],"type":"MAIN"}],"type":"TYPE2"},{"id":"3","children":[{"id":"4","children":[{}],"type":"MAIN"},{"id":"5","children":[{"id":"7","type":"TYPE2","children":[{"id":"8","type":"MAIN","children":[{}]}]}],"type":"MAIN"},{"id":"6","children":[{}],"type":"MAIN"}],"type":"TYPE2"}]; console.log(count_TypeX_Within_TypeY(arr_obj, "MAIN", "TYPE2"));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.