【问题标题】:Transform an array to an object with nested properties according to the array根据数组将数组转换为具有嵌套属性的对象
【发布时间】:2018-03-04 21:09:12
【问题描述】:

我有几个数组如下:

[ 'businessOpenAccount', 'accountSettings1.page.js' ]
[ 'businessOpenAccount', 'accountSettings2.page.js' ]
[ 'mainTest', 'test', 'test1.page.js' ]
[ 'mainTest', 'test', 'test2.page.js' ]

我的预期结果是以这种方式拥有一个对象:

{
  businessOpenAccount: {
    'accountSettings1.page.js': {},
    'accountSettings2.page.js': {}
  },
  mainTest: {
    test: {
      'test1.page.js': {},
      'test2.page.js': {}
    }
  }
}

所以实际上我想解析数组并构建一个嵌套对象以从它们返回,但一定要检查一个属性是否已经存在(因为从以前的数组定义)我不会覆盖它,而只是添加新的嵌套属性,尊重正确的嵌套顺序。

我尝试了几种使用reducereduceRight 和简单的forEach/for 循环的方法,但我仍然无法真正实现我想要的解决方案。

有什么建议吗?

这是迄今为止我最好的方法,但我覆盖了在多个数组上循环的属性(带有单个数组的示例):

const relevantFilePath = ['businessOpenAccount', 'accountSettings.page.js'];
let obj = {};
relevantFilePath.forEach((el, ind) => {
  if (ind === 0) {
    obj[el] = {};
    previousEl = obj[el];
  } else {
    previousEl[el] = {};
    previousEl = previousEl[el];
  }
});
console.log(obj);

【问题讨论】:

    标签: javascript node.js javascript-objects


    【解决方案1】:

    您可以迭代给定的数据并减少对象并获取具有拆分值的最后一项。

    var data = [['businessOpenAccount', 'accountSettings1.page.js'], ['businessOpenAccount', 'accountSettings2.page.js'], ['mainTest', 'test', 'test1.page.js'], ['mainTest', 'test', 'test2.page.js']],
        object = {};
        
    data.forEach(function (a) {
        var last = a.pop().split('.')[0];
        
        a.reduce(function (o, k) {
            return o[k] = o[k] || {};
        }, object)[last] = '';
    });
    
    console.log(object);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    不改变原始数据的 ES6

    var data = [['businessOpenAccount', 'accountSettings1.page.js'], ['businessOpenAccount', 'accountSettings2.page.js'], ['mainTest', 'test', 'test1.page.js'], ['mainTest', 'test', 'test2.page.js']],
        object = {};
        
    data.forEach(function (a) {
         var temp = a.slice(),
             last = temp.pop().split('.')[0];
        
        temp.reduce((o, k) => o[k] = o[k] || {}, object)[last] = '';
    });
    
    console.log(object);
    .as-console-wrapper { max-height: 100% !important; top: 0; }

    【讨论】:

    • 如果我说我真的很爱你,我是不是很粗鲁?谢谢一百万!
    【解决方案2】:

    一种递归解决方案,适用于任何深度无限的子数组。

    const a = [
        ['businessOpenAccount', 'accountSettings1.page.js'], 
        ['businessOpenAccount', 'accountSettings2.page.js'], 
        [ 'businessOpenAccount', 'test1', 
            [
                ['test2', 'test2.settings.page.js', 
                    [
                         ['test2', 'test2.settings.page.js'],
                         ['test3', 'test3.settings.page.js'],
                    ], 
                    'test4',
                ],
                ['test3', 'test3.settings.page.js'],
            ]
        ],
        ['mainTest', 'test', 'test1.page.js'], 
        ['mainTest', 'test', 'test2.page.js'],
    ];
    
    const result = {};
    
    const traverse = (result, arr) => {
        let firstEl = arr.shift();
        if (firstEl instanceof Array) {
            if (arr.length >= 1) {
                traverseTop(result, firstEl);
                return traverse(result, arr);
            } 
            return traverseTop(result, firstEl);
        }
        firstEl = firstEl.split('.')[0];
        result[firstEl] = arr.length >= 1 ? (result[firstEl] || {}) : '';
        if (arr.length >= 1) {
            return traverse(result[firstEl], arr);
        }
        return;
    };
    
    const traverseTop = (result, arr) => {
        arr.forEach((subArr) => {
            const firstEl = subArr.shift().split('.')[0];
            result[firstEl] = result[firstEl] || {};
            traverse(result[firstEl], subArr)
        });
        return result;
    };
    
    console.log(traverseTop(result, a));

    【讨论】:

    • 赞成另一个你的答案,这是完全正确的,也是这个问题的最佳答案,为了感谢你的努力,我真的很感激,我真的很感激,但是尼娜在这方面的解决方案case 更优雅,SO 的目的是为将来会遇到类似问题的其他用户提供最佳解决方案,因此在这种情况下只支持该问题。但再次感谢一百万
    • 你说得对,我发现另一个更优雅:)
    • 我已经更新了我的代码来处理嵌套结构。我喜欢递归:D
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-03
    • 1970-01-01
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2021-10-24
    相关资源
    最近更新 更多