将平面 json 结构转换为分层结构

Convert flat json structure to hierarchical

我无法解决这个问题。

我有一个看起来像

的设置对象
const setting = [
    {
        Key: 'root/',
        Value: null,
    },
    {
        Key: 'root/second-root/',
        Value: null,
    },
    {
        Key: 'root/second-root/names/',
        Value: null,
    },
    {
        Key: 'root/second-root/names/capital-letter',
        Value: true,
    },
    {
        Key: 'root/second-root/countries/',
        Value: null,
    },
    {
        Key: 'root/second-root/countries/enabledcountries',
        Value: 'US,UK,DK',
    },
    {
        Key: 'root/second-root/countries/async',
        Value: 'true',
    },
    {
        Key: 'root/second-root/countries/manual',
        Value: 'true',
    },
    {
        Key: 'root/second-root/countries/limit',
        Value: '4200',
    },
    {
        Key: 'root/second-root/names/onyl-last-name',
        Value: false,
    },
];

我需要将其转换为

const wantedResult = [
    {
        'root': {
            'Value': null,
            'second-root': {
                'Value': null,
                'names': {
                    'Value': null,
                    'capital-letter': {
                        'Value': true,
                    }
                    'onyl-last-name': {
                        'Value': false,
                    }
                },
                'countries': {
                    'Value': null,
                    'enabledcountries': {
                        Value: 'US,UK,DK',
                    },
                    'async': {
                        Value: 'true',
                    },
                    'manual': {
                        Value: 'true',
                    },
                    'limit': {
                        Value: '4200',
                    }
                }
            }
        }
    }
];

控制层次结构的是键属性。如果它以 / 结尾,则该项目是一个目录,否则它是一个值。

问题是平面结构不必 return 项目按正确顺序排列。与示例中一样,最后一项是 'root/second-root/names/onyl-last-name',,即使名称层次结构位于平面结构的开头。

我试过 array.reduce 的一种形式,但每次都卡住了。有人可以帮帮我吗

您可以迭代数组并获取不带最后一个斜杠的值,并将其拆分为对象的路径以分配值。

如有必要,将结果放入数组中。

forEach作品中

  • a destructuring assignment 用于从对象中获取键和值,

  • 在字符串末尾用空字符串查找斜杠的替换,

  • 用斜杠分割字符串,它returns一个没有斜杠的字符串数组,

  • 使用 Array#reduce 并将结果对象作为累加器。

    它在内部使用具有 logical OR || which check if the left side is a truthy 值的默认模式,如果没有,则分配一个对象。返回此值以用于检查下一个键。

  • 在迭代结束时它返回一个对象引用然后赋值。

var setting = [{ Key: 'root/', Value: null }, { Key: 'root/second-root/', Value: null }, { Key: 'root/second-root/names/', Value: null }, { Key: 'root/second-root/names/capital-letter', Value: true }, { Key: 'root/second-root/countries/', Value: null }, { Key: 'root/second-root/countries/enabledcountries', Value: 'US,UK,DK' }, { Key: 'root/second-root/countries/async', Value: 'true' }, { Key: 'root/second-root/countries/manual', Value: 'true' }, { Key: 'root/second-root/countries/limit', Value: '4200' }, { Key: 'root/second-root/names/onyl-last-name', Value: false }],
    result = {};

setting.forEach(({ Key, Value }) => Key
    .replace(/\/$/, '')
    .split('/')
    .reduce((o, k) => o[k] = o[k] || {}, result).Value = Value
);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }