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



我无法解决这个问题。

我有一个设置对象,看起来像

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

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

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

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

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

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

forEach作品

  • 用于从对象中获取键和值的解构赋值

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

  • 用斜杠拆分字符串,它返回一个没有斜杠的字符串数组,

  • Array#reduce 与结果对象一起使用作为累加器。

    在它内部使用带有逻辑 OR ||的默认模式,该模式检查左侧是否为真值,如果不是,则分配一个对象。返回此值以使用下一个键进行检查。

  • 在迭代结束时,它会重新调整对象引用,然后分配值。

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

最新更新