如何将单维json数组或对象更改为多维

时间:2017-06-24 20:21:39

标签: javascript jquery arrays json

使用Javascript或jQuery如何将单维数组更改为多维数组或嵌套数组。请假设嵌套可以更深层次。

这就是我获取数据的方式:

var beforeModifiedObj = [
  {
    id: '1',
    title: 'Awesome Group',
    type: '1',
    parentID: '0',
  },
  {
    id: '2',
    title: 'Rockers',
    type: '2',
    parentID: '0'
  },
  {
    id: '3',
    title: 'Dazzlers',
    type: '3',
    parentID: '0'
  },
  {
    id: '4',
    title: 'Rock-n-Rolla',
    type: '3',
    parentID: '0'
  },
  {
    id: '5',
    title: 'Child in Level Two - A',
    type: '2',
    parentID: '1',
  },
  {
    id: '6',
    title: 'Child in Level Three - A',
    type: '2',
    parentID: '5',
  },
  {
    id: '7',
    title: 'Child in Level Two - B',
    type: '2',
    parentId: '1'
  },
  {
    id: '8',
    title: 'Child in Level Three - B',
    type: '2',
    parentID: '5',
  }
];

处理后,需要如下所示:

var AfterModifiedObj = [
  {
    id: '1',
    title: 'Awesome Group',
    type: '1',
    parentID: '0',
    groups: [
      {
        id: '5',
        title: 'Child in Level Two - A',
        type: '2',
        parentID: '1',
        groups: [
          {
            id: '6',
            title: 'Child in Level Three - A',
            type: '2',
            parentID: '5',
          },
          {
            id: '8',
            title: 'Child in Level Three - B',
            type: '2',
            parentID: '5',
          }
        ]
      },
      {
        id: '7',
        title: 'Child in Level Two - B',
        type: '2',
        parentID: '1'
      }
    ]
  },
  {
    id: '2',
    title: 'Rockers',
    type: '2',
    parentID: '0'
  },
  {
    id: '3',
    title: 'Dazzlers',
    type: '3',
    parentID: '0'
  },
  {
    id: '4',
    title: 'Rock-n-Rolla',
    type: '3',
    parentID: '0'
  },
];

1 个答案:

答案 0 :(得分:1)

前一段时间,我需要一个类似的功能,我稍微适应了你的要求。我评论它并希望有所帮助。注意 beforeModifiedObj 元素,ID为7,在parentID中有拼写错误!

function findAndInsert(searchArray, toInsert) {
    // We use an internal function in order to avoid that a developer has to pass an additional
    // value to findAndInsert
    function findAndInsertAcc(searchArray, toInsert, isTopLevel) {
        var wasParentFound = false;
        for (var i = 0; i < searchArray.length; i++) {
            // Item was found => insert and create groups attribute
            if (searchArray[i].id == toInsert.parentID) {
                if (searchArray[i].groups === undefined) {
                    searchArray[i].groups = [];
                }
                searchArray[i].groups.push(toInsert);
                wasParentFound = true;
                break;
            } else if ('groups' in searchArray[i]) {
                // Recursively continue the search in the groups property
                wasParentFound = findAndInsertAcc(searchArray[i].groups, toInsert, false);
                if (wasParentFound) {
                    break;
                }
            }
        }
        // Insert into the top-level array if no item was found and we're in the top level
        if (!wasParentFound && isTopLevel) {
            searchArray.push(toInsert);
        }
        return wasParentFound;
    }

    findAndInsertAcc(searchArray, toInsert, true);
}

var afterModifiedObj = [];
beforeModifiedObj.forEach(function(next) {
    findAndInsert(afterModifiedObj, next);
});