将对象数组按角度推送到JSON

时间:2018-10-31 05:47:08

标签: angularjs json multidimensional-array

我确实有下面显示的JSON对象

[
  {
    "directories": [
      {
        "type": "folder",
        "name": "Real Estate",
        "order": "1",
        "delete": "false",
        "canModify": "true"
      },
      {
        "type": "folder",
        "name": "ABC",
        "order": "8",
        "delete": "false",
        "canModify": "true"
      },
      {
        "type": "folder",
        "name": "Legal",
        "order": "2",
        "delete": "false",
        "canModify": "false",
        "directories": [
          {
            "type": "subfolder",
            "name": "Lease",
            "order": "9",
            "delete": "false",
            "canModify": "false"
          }
        ]
      },
      {
        "type": "folder",
        "name": "Construction",
        "order": "4",
        "delete": "false",
        "canModify": "true"
      }
    ]
  }
]

我有一个对象数组,需要将其放入具有“名称:构造”的对象中。

我尝试了以下代码

var folderObj = {
                  "name": "",
                  "canModify":'true',
                  "type":'folder',
                  "delete":'false',
                   "order": $scope.directories.length + 1
                }
$scope.directories.push(folderObj);

我需要的实际输出是将对象推送到父文件夹中

  [
      {
        "directories": [
          {
            "type": "folder",
            "name": "Real Estate",
            "order": "1",
            "delete": "false",
            "canModify": "true"
          },
          {
            "type": "folder",
            "name": "ABC",
            "order": "8",
            "delete": "false",
            "canModify": "true"
          },
          {
            "type": "folder",
            "name": "Legal",
            "order": "2",
            "delete": "false",
            "canModify": "false",
            "directories": [
              {
                "type": "subfolder",
                "name": "Lease",
                "order": "9",
                "delete": "false",
                "canModify": "false"
              }
            ]
          },
          {
            "type": "folder",
            "name": "Construction",
            "order": "4",
            "delete": "false",
            "canModify": "true",
            "directories": [
              {
                "type": "subfolder",
                "name": "NewFolder",
                "order": "10",
                "delete": "false",
                "canModify": "false"
              }
            ]
          }
        ]
      }
    ]

由于我是新手,所以我无法弄清楚如何推动物体,因此,不胜感激。

1 个答案:

答案 0 :(得分:0)

您需要编写一个递归函数。尝试关注

let arr = [{"directories":[{"type":"folder","name":"Real Estate","order":"1","delete":"false","canModify":"true"},{"type":"folder","name":"ABC","order":"8","delete":"false","canModify":"true"},{"type":"folder","name":"Legal","order":"2","delete":"false","canModify":"false","directories":[{"type":"subfolder","name":"Lease","order":"9","delete":"false","canModify":"false"}]},{"type":"folder","name":"Construction","order":"4","delete":"false","canModify":"true"}]}];
let obj = {"type":"subfolder","name":"NewFolder","order":"10","delete":"false","canModify":"false"};
let label = "Construction";

/* Function takes 3 inputs
 * a - arr containing objects
 * l - label - unique identifier of the match
 * o - object that needs to be added */
function addObjectToTree(a, l, o) {
  // Iterate over arr
  for (let i = 0; i < a.length; i++) {
    let p = a[i]; // get the object
    if(p.name === l) { // if there is a match add to directories
      p.directories = (p.directories || []).concat([o]);
      return true;
    } else if(p.directories) { // else check if there are sub-folders
      // If there was a match in sub folders break the loop
      if(addObjectToTree(p.directories, l, o)) break;
    }
  }
}

addObjectToTree(arr, label, obj);
console.log(arr);