将对象的对象转换为数组的数组

问题描述

我正在尝试编写一个函数,将对象的对象转换为数组的数组。该数组应具有与对象中相同的子级。 对象的对象是这样的:-

world@myId1 : {

       north@myId2:{
           korea@myId21:{
             1:northKorea@myId211,2:southKorea@myId212
              }            
           },south@myId3:{
            India@myId31:{
                 1:nothindia@myId311,2: southindia@myId312
                }
             },west@myId4:{
           spain@myId41:{
            1:barcelona@myId411
            }
         },east@myId5:{
          UAE@myId51:{
           1:dubai@myId511
           }
         }
   }

我想把它改成

[{
  "name": "world","id": "myId1","nodes": [{
    "name": "north","id": "myId2","nodes": [{
      "name": "Korea","id": "myId21","nodes": [{
          "name": "northKorea","id": "myId211","nodes": []
        },{
          "name": "southKorea","id": "myId212","nodes": []
        }
      ]
    }]
  },{
    "name": "South","id": "myId3","nodes": [{
      "name": "India","id": "myId31","nodes": [{
          "name": "Southindia","id": "myId311",{
          "name": "northindia","id": "myId312","nodes": []
        }

      ]
    }]
  },{
    "name": "west","id": "myId4","nodes": [{
      "name": "Spain","id": "myId41","nodes": [{
        "name": "barcelona","id": "myId411","nodes": []
      }]
    }]
  }]
},{
  "name": "East","id": "myId5","nodes": [{
    "name": "UAE","id": "myId51","nodes": [{
      "name": "dubai1","id": "myId511","nodes": []
    }]
  }]
}];

我创建了一个递归函数,但它非常慢,因为对象的实际对象非常大。 我以为angularjs会异步调用函数,但事实并非如此。

   fillChid = function (array,data) = {
    var nodeObj = {
      "name": "name","nodes": []
    };
    //caliculate name and id
    var childindex = -1;
    angular.forEach(data,function (value,key) {
      array.push(presentNode);
      childindex++;
      if (isNaN(key))
        fillChild(array[childindex].nodes,value,presentNode.parentString);

    });

  }

我需要这种数据类型来显示树形菜单JSFIDDLE 用于树形菜单代码

解决方法

您可以采用递归方法并返回键或值的信息,具体取决于是否具有子对象。

const
    getObject = (identifier,nodes = []) => {
        const [name,id] = identifier.split('@');
        return { name,id,nodes };
    },getArray = object => Object.entries(object).map(([k,v]) => v && typeof v === 'object'
        ? getObject(k,getArray(v))
        : getObject(v)
    ),data = { 'world@myId1': { 'north@myId2': { 'korea@myId21': { 1: 'northKorea@myId211',2: 'southKorea@myId212' } },'south@myId3': { 'India@myId31': { 1: 'nothIndia@myId311',2: 'southIndia@myId312' } },'west@myId4': { 'spain@myId41': { 1: 'barcelona@myId411' } },'east@myId5': { 'UAE@myId51': { 1: 'dubai@myId511' } } } },result = getArray(data);
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

更快

const
    getObject = (identifier,getArray = object => {
        const result = [];
        for (const [k,v] of Object.entries(object))
            result.push(v && typeof v === 'object'
                ? getObject(k,getArray(v))
                : getObject(v)
            );
        return result;
    },result = getArray(data);
  
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }