需要帮助递归遍历Javascript对象的所有级别

我正在尝试创建一个对象,并且在该对象内将是name和下的其他对象数组children。我真的想从另一个对象创建层次结构。


我试图创建一个递归函数,但最终得到的是垂直切片而不是整个图片。我不确定如何调整我的递归以回溯添加迭代通过其他水平对象。


buildHierarchy(json) {

  console.log("Entered Build Hierarchy");


  let newObject;

  newObject = this.buildChildren(json);


  console.log(newObject);

  return newObject


}

buildChildren(json) {

  let returnObject;

  for (var key in json) {

    returnObject = {

      name: key,

      children: []

    };

    var subObject = json[key];


    if (Array.isArray(subObject)) {

      returnObject = {

        name: key,

        _proficiency: subObject

      }

    } else {

      returnObject["children"].push(this.buildChildren(subObject))


    }

  }

  return returnObject;

}


假设您在下面有这个json文件


{users: 

  {sandy: {

    posts: [

      { title: 'Bar', comments: [ 'Ok' ] },

    ]

    followers: [

      { name: 'Foo' },

    ]

  }

 ron: {

    photos: [

      { title: 'Foo', comments: [ 'Ok' ] },

    ]

  }

 }

}

我正在寻找这样的东西...


{

  name: "users",

  children: [

    {

      name: "sandy",

      children: [

        {

          name: "posts",

          children: [

            {

              name: "Bar",

              comments: "OK"

            }],

        { name: "followers"

          children: [

            {

              name: "Foo"

            }

          ]

        } 

      }

    ]

  },

    {

      name: "ron",

      photos: [

        {

          name: "photos",

          children: [

            {

              name: "Foo",

              comments: "OK"

            }

          ]

        }

      ]

    }

  ]

}


aluckdog
浏览 212回答 2
2回答

莫回无

从我所看到的输出中,我得出了这些推论:如果子对象是数组,则将其盲目复制到childrenkey如果子元素是一个对象,则将它们更改为&nbsp;{name: <KEY>, children: <VALUE>}function buildChildren(json) {&nbsp; let returnObject = [];&nbsp; &nbsp; if (typeof json !== 'object') {&nbsp; &nbsp; return json;&nbsp; }&nbsp; for (var key in json) {&nbsp; &nbsp; if (Array.isArray(json)) {&nbsp; &nbsp; &nbsp; returnObject.push(buildChildren(json[key]));&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; returnObject.push({&nbsp; &nbsp; &nbsp; &nbsp; name: key,&nbsp; &nbsp; &nbsp; &nbsp; children: buildChildren(json[key])&nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; }&nbsp; }&nbsp; return returnObject;}

陪伴而非守候

function buildHierarchy(json) {&nbsp; &nbsp; console.log("Entered Build Hierarchy");&nbsp; &nbsp; let newObject;&nbsp; &nbsp; newObject = buildChildren(json);&nbsp; &nbsp; console.log(newObject);}function buildChildren(json) {&nbsp; &nbsp; if (Array.isArray(json)) {&nbsp; &nbsp; &nbsp; &nbsp; return {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; _proficiency: json&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; var children = Object.keys(json);&nbsp; &nbsp; let final = [];&nbsp; &nbsp; for (var i = 0; count = children.length, i < count; i++) {&nbsp; &nbsp; &nbsp; &nbsp; let result = {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; name: children[i]&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; let d = buildChildren(json[children[i]]);&nbsp; &nbsp; &nbsp; &nbsp; if (d._proficiency) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result._proficiency = d._proficiency;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; result.children = d;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; final.push(result);&nbsp; &nbsp; }&nbsp; &nbsp; return final;}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript