【问题标题】:Is there a better way to iterate through an object of unknown depth than using map statements nested to the maximum possible depth?有没有比使用嵌套到最大可能深度的 map 语句更好的方法来遍历未知深度的对象?
【发布时间】:2021-07-27 04:01:02
【问题描述】:

我嵌套了不同的、未知深度的员工对象。每个对象都有一个children 属性,它是向该员工报告的员工对象的数组。这些子对象具有与顶级对象相同的属性,并且它们自己的children 属性中可能有也可能没有雇员对象。

我需要遍历每个员工对象的员工对象数组,并将这些对象中的每一个添加到两个不同数组之一中,具体取决于对象在其自己的“子”属性中是否有其他员工对象。这些数组也是员工对象的属性。 “子”数组为空的员工将被添加到其父员工的nonManagersUnder 数组中,而在其children 数组中包含对象的员工将被添加到managersUnder 数组中。

嵌套的员工对象如下所示:

 {
                id: "n1",
                "isActive": true,
                age: 38,
                name: "Barb Smith",
                "phone": "+1 (882) 547-3581",
                "hired": "2016-08-08T12:46:19 +07:00",
                children: [
                  {
                      id: "n10",
                      "isActive": true,
                      age: 37,
                      name: "Elsie MacDonald",
                      "phone": "+1 (958) 558-2389",
                      "hired": "2015-08-15T04:44:49 +07:00",
                      children: [
                        
                      ]
                    },
                    {
                      id: "n11",
                      "isActive": true,
                      age: 29,
                      name: "Peter Chen",
                      "phone": "+1 (881) 574-3927",
                      "hired": "2015-02-16T12:11:11 +08:00",
                      children: [
                        
                      ]
                    },
                    {
                      id: "n12",
                      "isActive": true,
                      age: 32,
                      name: "Ty Wilder",
                      "phone": "+1 (990) 506-2830",
                      "hired": "2019-09-17T06:29:16 +07:00",
                      children: [
                        
                      ]
                    }
}

这是一个非常简单的例子,因为我不想在我的帖子中放几百行长的东西,但结构是一样的。想象一下,每个次要员工对象都有自己的子对象。

您会注意到nonManagersUndermanagersUnder 数组并不是员工对象的属性。那是因为在我当前的解决方案中,它们是动态分配的。

解决方案如下:

countManagers = (employee) => {
  let midManagers = []
  let nonManagers = []
  employee.children.map(child =>{
      if(child.children.length == 0) {
          nonManagers.push(child);
      }else {
          midManagers.push(child);
          child.children.map(grandChild => {
              if(grandChild.children.length == 0){
                  nonManagers.push(grandChild);
              }else {
                  midManagers.push(grandChild);
                  grandChild.children.map(greatGrand => {
                      if(greatGrand.children.length == 0){
                          nonManagers.push(greatGrand)
                      } else {
                          midManagers.push(greatGrand);
                          greatGrand.children.map(grand3 => {
                             if(grand3.children.length==0){
                                 nonManagers.push(grand3);
                             } else {
                                 midManagers.push(grand3);
                                 grand3.children.map(grand4 => {
                                     if(grand4.children.length==0){
                                         nonManagers.push(grand4);
                                     } else {
                                         midManagers.push(grand4);
                                     }
                                 })
                             }
                               
                          })
                      }
                  })
              }
          })
      }
  })
  console.log(midManagers);
  // console.log(nonManagers);
  employee.managersUnder = (midManagers);
  employee.nonManagersUnder=(nonManagers)
}

如您所见,它只是嵌套的映射运算符和一些条件,嵌套到员工对象可以嵌套的最大深度。这个解决方案确实有效,但非常难看,我几乎可以肯定有更好的方法来做到这一点。更好的解决方案适用于任何深度的对象。这仅适用于深度等于或小于嵌套映射运算符数量的对象。

【问题讨论】:

  • 递归FTW!!

标签: javascript arrays reactjs nested iteration


【解决方案1】:

我想刷新一些递归的东西,并提出了一个解决方案供您查询。

const values = [{
  id: "n1",
  children: [{
      id: "n10",
      children: [{
        id: "n100",
        children: []
      }, ]
    },
    {
      id: "n11",
      children: []
    },
    {
      id: "n12",
      children: []
    }
  ]
}]

const getAllManagers = (employees) => {
  return employees.reduce((acc, emp) => {
    return acc.concat(emp.children.length > 0 ? [emp, ...getAllManagers(emp.children)] : [])
  }, [])
}

const getAllNonManagers = (employees) => {
  return employees.reduce((acc, emp) => {
    return acc.concat(emp.children.length > 0 ? getAllNonManagers(emp.children) : emp)
  }, [])
}

console.log("Managers: ", getAllManagers(values))
console.log("NonManagers:", getAllNonManagers(values))

【讨论】:

    猜你喜欢
    • 2014-07-06
    • 1970-01-01
    • 2020-03-28
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2015-11-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多