簡體   English   中英

有沒有比使用嵌套到最大可能深度的 map 語句更好的方法來遍歷未知深度的 object?

[英]Is there a better way to iterate through an object of unknown depth than using map statements nested to the maximum possible depth?

我嵌套了不同的、未知深度的員工對象。 每個 object 都有一個children屬性,它是向該員工報告的員工對象的數組。 這些子對象具有與頂級 object 相同的屬性,並且它們自己的children屬性中可能有也可能沒有雇員對象。

我需要通過每個員工對象的員工對象數組 go 並將這些對象中的每一個添加到兩個不同的 arrays 之一,具體取決於 object 是否有其他員工對象。 這些 arrays 也是員工對象的屬性。 具有空“子” arrays 的員工將被添加到其父員工的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 arrays 不是員工對象的屬性。 那是因為在我當前的解決方案中,它們是動態分配的。

這是解決方案:

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)
}

如您所見,它只是嵌套了 map 運算符和一些條件,嵌套到員工 object 可以嵌套的最大深度。 這個解決方案確實有效,但非常難看,我幾乎可以肯定有更好的方法來做到這一點。 更好的解決方案適用於任何深度的 object。 這僅適用於深度等於或小於嵌套 map 運算符數量的對象。

我想刷新一些遞歸的東西,並為您提供了一個解決方案供您查詢。

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))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM