I have an array of objects in this format:
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: {}
}
];
And I need to get a result in this format:
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: {}
}
]
}
];
I am trying to write a function groupArray which will group array of object based on array of keys provided, e.g., groupArray(arr, ['parentKey.myKey1', 'parentKey.myKey2', 'parentKey.myKey3', 'parentKey.myKey4']) but haven't succeeded. Here is the jsfiddle with my code. All entries are currently placed in resulting array without being grouped.
Could you give me hint how to fix my function.
resultincludes 2 objects from the original arrayarr. I am trying to archive something similar to what group-array package does but without nesting.