【问题标题】:covert Object of Objects to Array of Arrays将对象对象转换为数组数组
【发布时间】:2021-05-03 08:30:03
【问题描述】:

我正在尝试编写一个函数,将对象的对象转换为数组的数组。该数组应具有与对象相同的子级。 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
           }
         }
   }

我希望它变成

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

我创建了一个递归函数,但它非常慢,因为对象的实际对象非常大。 我以为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 用于树形菜单代码。

【问题讨论】:

    标签: javascript arrays angularjs javascript-objects


    【解决方案1】:

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

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

    【讨论】:

    • Answer 正在运行,但仍然很慢,几乎和我的函数一样快。
    • 看起来问题出在使用递归函数上,当数据最后时它变慢了。我的浏览器甚至挂起。有没有办法不使用递归函数?
    • 您可以排队并收集目标和数据并采取线性方法。
    • 我不太明白,你能提供一个sn-p开始吗?
    猜你喜欢
    • 2018-12-04
    • 2021-06-16
    • 1970-01-01
    • 2019-10-22
    • 2019-07-29
    • 1970-01-01
    • 2020-06-15
    相关资源
    最近更新 更多