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

时间:2018-01-10 15:26:04

标签: javascript immutable.js

我无法理解这个问题。

我有一个看似

的设置对象
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',,即使名称为hiarchy位于平面结构的开头。

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

1 个答案:

答案 0 :(得分:2)

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

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

forEach作品中

  • destructuring assignment用于获取对象的键和值,

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

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

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

    其中使用带logical OR ||的默认模式,检查左侧是否为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; }