简体   繁体   English

对象的隐蔽 Object 到 Arrays 数组

[英]covert Object of Objects to Array of Arrays

I am trying to write a function that converts and object of objects into array of array.我正在尝试编写一个 function 将对象的 object 转换为数组数组。 The array should have same child as in the objects.该数组应具有与对象相同的子级。 The object of object is like:- object 的 object 是这样的:-

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
           }
         }
   }

I want it transformed to我希望它转变为

[{
  "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",
          "nodes": []
        },
        {
          "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": []
    }]
  }]
}];

I created a recursive function but it is very slow, as the actual object of objects is very large.我创建了一个递归 function 但它非常慢,因为对象的实际 object 非常大。 I thought that angularjs will call the function asynchronously, but that is not the case.我以为 angularjs 会异步调用 function ,但事实并非如此。

   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);

    });

  }

I need this data type to show a Tree menu, JSFIDDLE for tree menu code.我需要这种数据类型来显示树形菜单, JSFIDDLE用于树形菜单代码。

You could take a recursive approach and return either the infdormation of the key or value, depending on having subobjects.您可以采用递归方法并返回键或值的信息,具体取决于具有子对象。

 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; }

A faster one一个更快的

 const getObject = (identifier, nodes = []) => { const [name, id] = identifier.split('@'); return { name, id, nodes }; }, 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; }, 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; }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM