Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/448.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 将对象数组分组为特定格式_Javascript_Arrays_Object_Grouping - Fatal编程技术网

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

Javascript 将对象数组分组为特定格式,javascript,arrays,object,grouping,Javascript,Arrays,Object,Grouping,我有一个如下格式的对象数组: const arr = [ { parentKey: { myKey1: 'someStringA', myKey2: 'anotherStringA', myKey3: false, myKey4: true }, anotherKey: {} }, { parentKey: {

我有一个如下格式的对象数组:

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: {}
    }
];
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: {}
            }
        ] 
    }
];
我需要得到以下格式的结果:

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: {}
    }
];
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: {}
            }
        ] 
    }
];
我正在尝试编写一个函数groupArray,它将根据提供的键数组对对象数组进行分组,例如,
groupArray(arr,['parentKey.myKey1','parentKey.myKey2','parentKey.myKey3','parentKey.myKey4'])
。这是我的密码。所有条目当前都放置在结果数组中,而不进行分组


你能告诉我如何修复我的功能吗

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

函数组数组(数组,键){
函数getValue(对象、路径){
返回路径.split('..).reduce((o,k)=>(o |{})[k],对象);
}
var hash=Object.create(null),
结果=[];
array.forEach(函数(o){
var key=keys.map(k=>getValue(o,k)).join(“|”);
如果(!哈希[键]){
hash[key]={props:Object.assign(…keys.map(k=>({[k.split('.').pop()]:getValue(o,k)})),条目:[]};
result.push(散列[键])
}
hash[key].entries.push(o);
});
返回结果;
}
var数组=[{parentKey:{myKey1:'sometherstringa',myKey2:'anotherStringA',myKey3:false,myKey4:true},anotherKey:{},{parentKey:{myKey1:'sometherstringa',myKey2:'anotherStringA',myKey3:true,myKey4:false},anotherKey:{},{parentKey:{myKey1:'sometherstringa',myKey2:'anotherStringA',myKey3:true,myKey4:false},{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(数组,['parentKey.myKey1','parentKey.myKey2','parentKey.myKey3','parentKey.myKey4'];
控制台日志(结果)

.as控制台包装{max height:100%!important;top:0;}
您好,不知道您的意思,您正在尝试将对象分组在一起吗?您提供的结果格式只是重新构造原始列表,没有分组。条目按提供的键分组。如果检查结果,您将看到
结果的第二个对象包含原始数组
arr
中的两个对象。我正在尝试归档一些类似的东西,但没有嵌套。