javascript - 将递归 json 对象转换为另一个 json 对象

标签 javascript json recursion

需要帮助将递归 json 对象转换为另一个 json 对象。

我的输入对象是这样的:

{  
  person: 
  {
       name: 'John Henry',
       age: 63
  },
  children: 
  [
        {
            person:
            {
                name: 'Paul Henry',
                age: 40
            },
            children: 
            [
                {
                    person:
                    {
                        name: 'Tom Henry',
                        age: 10
                    }
                }
                {
                    person:
                    {
                        name: 'Mike Henry',
                        age: 12
                    }
                }
            ]
        },
        {
            person:
            {
                name: 'Wilson Henry',
                age: 30
            }
        },
        {
            person:
            {
                name: 'Richard Henry',
                age: 59
            }
        }
  ]
}

我想要的输出是:

[{
          name: 'John Henry',
          attributes: { age: 63 },
          children: 
          [
            {
                name: 'Paul Henry',
                attributes: { age: 40 },
                children: [
                  {
                      name: 'Tom Henry',
                      attributes: { age: 10 }
                  },
                  {
                      name: 'Mike Henry',
                      attributes: { age: 12 }
                  }
                ]
            },
            {
                name: 'Wilson Henry',
                attributes: { age: 30 }
            },
            {
                name: 'Richard Henry',
                attributes: { age: 59 }
            }
         ]
}];

这是我到目前为止尝试过的方法,但卡在了递归部分。不确定如何将所有内容组合在一起。:

var tree = {};
var getFamilyTree = function (input) {

  tree.name = input.person.name;
  tree.attributes = { 'age': input.person.age };
  tree.children = [];

  input.children.forEach(function (child) {
      //not sure how to recursively go through the child nodes.
  });
};

最佳答案

在函数的开头创建一个新的输出对象。

对于每个 child ,对 child 再次调用 getFamilyTree() - 这将是新的 input。将该调用的结果附加到您的新子列表中。

返回你新创建的对象。

类似于:

function getFamilyTree(input)
{
  var newb = { 'attributes': {} };

  for ( f in input.person )    // we may have attributes other than "age"
    if (f == 'name')
      newb.name = input.person[f];
    else
      newb.attributes[f] = input.person[f];

  if (input.children && input.children.length)
    {
      newb.children = [];

      for ( var i = 0; i < input.children.length; ++i )
        {
          newb.children.push(getFamilyTree(input.children[i]));
        }
    }

  return newb;
}

var orig = {  
  person: 
  {
       name: 'John Henry',
       age: 63
  },
  children: 
  [
        {
            person:
            {
                name: 'Paul Henry',
                age: 40
            },
            children: 
            [
                {
                    person:
                    {
                        name: 'Tom Henry',
                        age: 10
                    }
                },
                {
                    person:
                    {
                        name: 'Mike Henry',
                        age: 12
                    }
                }
            ]
        },
        {
            person:
            {
                name: 'Wilson Henry',
                age: 30
            }
        },
        {
            person:
            {
                name: 'Richard Henry',
                age: 59
            }
        }
  ]
};


function getFamilyTree(o)
{
  var newb = { 'attributes': {} };
  
  for ( f in o.person )    // we may have attributes other than "age"
    if (f == 'name')
      newb.name = o.person[f];
    else
      newb.attributes[f] = o.person[f];
  
  if (o.children && o.children.length)
    {
      newb.children = [];
      
      for ( var i = 0; i < o.children.length; ++i )
        {
          newb.children.push(getFamilyTree(o.children[i]));
        }
    }
  
  return newb;
}


console.log( JSON.stringify( getFamilyTree(orig), null, "  " ) );

关于javascript - 将递归 json 对象转换为另一个 json 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27952865/

相关文章:

json - NSTextview如何显示json格式的字符串

java - 处理 JSONObject 以获取项目

PHP递归导航

Javascript 使用 (+) 和 (-) 自动计算

Javascript图像数据转多维数组性能问题

JSONDecoder 无法处理空响应

c# - 返回某些东西时完全停止递归

javascript - 为什么 PopUp Modal Bootstrap 没有出现在 Angular 4 中

Javascript Canvas 如何从图像中删除形状或区域?

c - 递归地反转链表数组并不是按顺序反转所有节点