【问题标题】:How to pull out relationships in nested JS object?如何拉出嵌套 JS 对象中的关系?
【发布时间】:2020-11-07 18:15:44
【问题描述】:

我有一个嵌套的(最多 21 个深度)JSON,格式如下:

{
        "name": "Unknown Wife",
        "id": 341,
        "house": "Tyrell",
        "gender": "Female",
        "partnerId": 340,
        "hasParnter": true,
        "noParent": true,
        "children": [{
            "name": "Olymer",
            "id": 342,
            "house": "Tyrell",
            "gender": "Male",
            "partnerId": 343,
            "hasParnter": true,
            "isSource": true
        }, {
            "name": "Lysa Meadows",
            "id": 343,
            "house": "Tyrell",
            "gender": "Female",
            "partnerId": 342,
            "hasParnter": true,
            "noParent": true,
            "children": [{
                "name": "Raymund",
                "id": 344,
                "house": "Tyrell",
                "gender": "Male",
                "isSource": true
            }, {
                "name": "Rickard",
                "id": 345,
                "house": "Tyrell",
                "gender": "Male",
                "isSource": true
            }, {
                "name": "Megga",
                "id": 346,
                "house": "Tyrell",
                "gender": "Female",
                "isSource": true
            }]

我正在尝试将这些数据转换为格式

{name: "someName", children:["all","children","deeper","in","object"]},

据推测,原始对象的最高级别成员将拥有最长的子数组。在一个血统中,随着您深入家族树,这将减少。

以递归方式进入家谱并返回上述格式的新对象的最佳途径是什么?

【问题讨论】:

  • 该数组的更好名称是 descendants 而不是 children
  • 如果叶子也有一个children数组,一个空的就是
  • 请澄清输出格式,unknown wife 是否会将 olymer、lysa、raymund 和 rickard 都视为“孩子”?
  • 抱歉,是的,所以第一级个人可能有 x 个后代。下一个最低的将包括 x-(至少)1

标签: javascript arrays json sorting data-manipulation


【解决方案1】:

我不太清楚你的问题到底是什么期望的结果,但它肯定是你所追求的递归,我会假设你想要这样的东西

obj={ "name": "Unknown Wife", "id": 341, "house": "Tyrell", "gender": "Female", "partnerId": 340, "hasParnter": true, "noParent": true, "children": [{ "name": "Olymer", "id": 342, "house": "Tyrell", "gender": "Male", "partnerId": 343, "hasParnter": true, "isSource": true }, { "name": "Lysa Meadows", "id": 343, "house": "Tyrell", "gender": "Female", "partnerId": 342, "hasParnter": true, "noParent": true, "children": [{ "name": "Raymund", "id": 344, "house": "Tyrell", "gender": "Male", "isSource": true }, { "name": "Rickard", "id": 345, "house": "Tyrell", "gender": "Male", "isSource": true }, { "name": "Megga", "id": 346, "house": "Tyrell", "gender": "Female", "isSource": true }] }] }
  results=[]
  children=[]
  function myfunc(myObj){
if(myObj.name){
  myObj.children.forEach(o=>{
    if(!o.children){
      children.push(o)
      results.push({name:myObj.name,children:children})
    }
    else {
      children.push({name:o.name,id:o.id,house:o.house,gender:o.gender,partnerId:o.partnerId,hasParnter:o.hasParnter,noParent:o.noParent})}
  })
    
} 
 if(Array.isArray(myObj.children)){
   for(let i=0; i<myObj.children.length; i++){
    if(!myObj.children[i].children) continue
    if(myObj.children[i].name) results.push({name:myObj.children[i].name,children:[myObj.children[i].children]})
      
      else myfunc(myObj.children[i])
  }
}
  }
  myfunc(obj)
  console.log(results)

【讨论】:

    【解决方案2】:

    你应该递归地使用 Reduce。

    var person = {"name":"Unknown Wife","id":341,"house":"Tyrell","gender":"Female","partnerId":340,"hasParnter":true,"noParent":true,"children":[{"name":"Olymer","id":342,"house":"Tyrell","gender":"Male","partnerId":343,"hasParnter":true,"isSource":true},{"name":"Lysa Meadows","id":343,"house":"Tyrell","gender":"Female","partnerId":342,"hasParnter":true,"noParent":true,"children":[{"name":"Raymund","id":344,"house":"Tyrell","gender":"Male","isSource":true},{"name":"Rickard","id":345,"house":"Tyrell","gender":"Male","isSource":true},{"name":"Megga","id":346,"house":"Tyrell","gender":"Female","isSource":true}]}]};
                
    
    function printAll(p1) {
        print(p1);
      if (p1.children) {
        p1.children.forEach(c => printAll(c))
      }
    }
    
    function print(p1) {
    console.log({person: p1.name, descendents: (p1.children || []).reduce(GetChildren, [])});
    }
    
    
    function GetChildren(children, child) {
      return children.concat(child.name).concat((child.children || []).reduce(GetChildren, []));
    }
    <button onclick="print(person)">Print main person</button>
    
    <button onclick="printAll(person)">Print for all</button>

    【讨论】:

    • 有趣 - 不知道 reduce。您的示例效果很好。奇怪的是,我认为形式相同的数据只是返回第一个个体。
    猜你喜欢
    • 1970-01-01
    • 2023-01-14
    • 2021-01-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-24
    • 2016-09-16
    相关资源
    最近更新 更多