简体   繁体   English

如何使用 react 和 javascript 在对象数组中查找某些 object 属性的计数?

[英]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.

相关问题 如何使用 javascript 从对象数组中查找其中一项是否已完成属性值为 false 并做出反应? - How to find if one of the items have completed property with value false from an array of objects using javascript and react? 如何使用 javascript 从与 id 匹配的对象的递归数组中找到 object 并做出反应? - How to find an object from recursive array of objects that is matching with the id using javascript and react? 如何循环对象的javascript object并找到一个属性 - How to loop javascript object of objects and find a property 如何使用 javascript 在数组中查找特定 object 的计数? - How to find the count of particular object in array using javascript? 如何在对象数组中查找和编辑对象的属性? - How to find and edit property of object in the array of an objects? 在另一个数组内的JavaScript对象数组中按属性查找对象 - Find object by property in an array of JavaScript objects inside another array 如何在 javascript 中按属性在数组中查找 object? - How to find object in array by property in javascript? 使用属性值从对象数组中查找并返回 object - Find and Return an object from an array of array of objects using a property value 在Javascript中,如何在满足某些条件的同时在具有最高特定属性值的数组中查找对象 - In Javascript how to find the object in an array that has the highest valued specific property while meeting some conditions 如果某些数组元素未定义,如何在对象数组中查找对象? - How to find an object in an array of objects if some array elements are undefined?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM