Previously, structure of my array was:
[
{
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
{
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
.....
]
I have created a function called flatToHierarchy to group the elements in an array. Here is the code:
export const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
const items = accumulator;
const propValue = currentItem[prop];
items[propValue] = items[propValue]
? [...items[propValue], currentItem]
: [currentItem];
return items;
}, {});
Now if I call it like:
flatToHierarchy(elements, 'kind')
It works fine.
Now, structure of my array is changed. Now it looks like:
[
{
"entity": {
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
entity_id: "jj98834jfj983"
},
{
"entity": {
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
entity_id: "jj98834jfdf83"
},
.....
]
So, now for grouping array by element kind, I call it this way:
flatToHierarchy(elements, 'entity.kind')
Now, it yields me something like:
{
undefined: [....element1, ....element2 ]
}
But what I want is:
{
PEAK_EXPERIENCE: [....element1, ....element2 ]
}
I have dug deeper in to this and found that array cannot be accessed this way:
array['something.something']
It needs to be accessed this way:
array['something']['something']
So, I changed my function but I am stuck. Here is my changed function:
export const flatToHierarchy = (arr, prop) => arr.reduce((accumulator, currentItem) => {
const items = accumulator;
const props = prop.split('.').map(p => [p]);
console.log(props);
const propValue = currentItem[prop];
console.log(currentItem...props); // how to spread the array here?
items[propValue] = items[propValue]
? [...items[propValue], currentItem]
: [currentItem];
return items;
}, {});
Update:
Sample of current input:
[
{
"entity": {
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
entity_id: "jj98834jfj983"
},
{
"entity": {
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
entity_id: "jj98834jfdf83"
},
{
"entity": {
"id": "Ddfwe23224594",
"kind": "PEAK_EXPERIENCE2",
"title": "IP - Collective Identity 6",
},
entity_id: "jj98834jfdf33"
},
]
sample of needed output:
{
PEAK_EXPERIENCE: [
{
"entity": {
"id": "Ddfwe23224533",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity",
},
entity_id: "jj98834jfj983"
},
{
"entity": {
"id": "Ddfwe23224534",
"kind": "PEAK_EXPERIENCE",
"title": "IP - Collective Identity 2",
},
entity_id: "jj98834jfdf83"
},
],
PEAK_EXPERIENCE2: [
{
"entity": {
"id": "Ddfwe23224594",
"kind": "PEAK_EXPERIENCE2",
"title": "IP - Collective Identity 6",
},
entity_id: "jj98834jfdf33"
},
],
}
Update 2:
While making changes to the required function, I need to keep in mind that this function will always have the signature as specified below:
flatToHierarchy(array, string);
example:
flatToHierarchy(elements, 'kind');
another example:
flatToHierarchy(elements, 'entity.kind');