将对象数组分组为特定格式

时间:2017-08-04 06:27:55

标签: javascript arrays object grouping

我有这种格式的对象数组:

webpack

我需要以这种格式得到一个结果:

const arr = [
    {
        parentKey: {
            myKey1: 'someStringA',
            myKey2: 'anotherStringA',
            myKey3: false,
            myKey4: true
        },
        anotherKey: {}
    },
    {
        parentKey: {
            myKey1: 'someStringA',
            myKey2: 'anotherStringA',
            myKey3: true,
            myKey4: false
        },
        anotherKey: {}
    },
    {
        parentKey: {
            myKey1: 'someStringA',
            myKey2: 'anotherStringA',
            myKey3: true,
            myKey4: false
        },
        anotherKey: {}
    },
    {
        parentKey: {
            myKey1: 'someStringA',
            myKey2: 'anotherStringA',
            myKey3: true,
            myKey4: true
        },
        anotherKey: {}
    },
    {
        parentKey: {
            myKey1: 'someStringA',
            myKey2: 'anotherStringB',
            myKey3: true,
            myKey4: true
        },
        anotherKey: {}
    },
    {
        parentKey: {
            myKey1: 'someStringB',
            myKey2: 'anotherStringB',
            myKey3: false,
            myKey4: true
        },
        anotherKey: {}
    }
];

我正在尝试编写一个函数groupArray,它将根据提供的键数组对对象数组进行分组,例如const result = [ { props: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: false, myKey4: true }, entries: [ { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: false, myKey4: true }, anotherKey: {} } ] }, { props: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: false }, entries: [ { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: false }, anotherKey: {} }, { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: false }, anotherKey: {} } ] }, { props: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: true }, entries: [ { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: true }, anotherKey: {} } ] }, { props: { myKey1: 'someStringA', myKey2: 'anotherStringB', myKey3: true, myKey4: true }, entries: [ { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringB', myKey3: true, myKey4: true }, anotherKey: {} } ] }, { props: { myKey1: 'someStringB', myKey2: 'anotherStringB', myKey3: false, myKey4: true }, entries: [ { parentKey: { myKey1: 'someStringB', myKey2: 'anotherStringB', myKey3: false, myKey4: true }, anotherKey: {} } ] } ]; 但是没有成功。这是我的代码jsfiddle。所有条目当前都放在结果数组中而不进行分组。

你能不能给我提示如何修复我的功能。

1 个答案:

答案 0 :(得分:1)

您可以使用哈希表对相同的组进行分组,并使用函数从对象中获取值,并使用它通过嵌套属性的路径。



function groupArray(array, keys) {
    function getValue(object, path) {
        return path.split('.').reduce((o, k) => (o || {})[k], object);
    }

    var hash = Object.create(null),
        result = [];

    array.forEach(function (o) {
        var key = keys.map(k => getValue(o, k)).join('|');
        if (!hash[key]) {
            hash[key] = { props: Object.assign(...keys.map(k => ({ [k.split('.').pop()]: getValue(o, k) }))), entries: [] };
            result.push(hash[key])
        }
        hash[key].entries.push(o);
    });
    return result;
}

var array = [{ parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: false, myKey4: true }, anotherKey: {} }, { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: false }, anotherKey: {} }, { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: false }, anotherKey: {} }, { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: true, myKey4: true }, anotherKey: {} }, { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringB', myKey3: true, myKey4: true }, anotherKey: {} }, { parentKey: { myKey1: 'someStringB', myKey2: 'anotherStringB', myKey3: false, myKey4: true }, anotherKey: {} }],
    result = groupArray(array, ['parentKey.myKey1', 'parentKey.myKey2', 'parentKey.myKey3', 'parentKey.myKey4']);

console.log(result);

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