My array consists of objects structured like this:
var exampleArray = [{
name: 'name1',
subname: 'subname1',
symbolname: 'symbol1'
},
{
name: 'name1',
subname: 'subname11',
symbolname: 'symbol11'
},
{
name: 'name2',
subname: 'subname2',
symbolname: 'symbol2'
},
{
name: 'name2',
subname: 'subname22',
symbolname: 'symbol22'
},
{
name: 'name3',
subname: 'subname3',
symbolname: 'symbol3'
},
{
name: 'name3',
subname: 'subname33',
symbolname: 'symbol33'
}];
I aim to transform this array into a hierarchical object, where the 'name' property will be the parent of 'subname', and 'subname' will be the parent of 'symbolname'. Here is an example of the desired structure:
result = {
name1: {
subname1: [symbolname1],
subname11: [symbolname11]
},
name2: {
subname2: [symbolname2],
subname22: [symbolname22]
},
name3: {
subname3: [symbolname3],
subname33: [symbolname33]
}
}
I attempted to use the reduce method in my code as shown below:
exampleArray.reduce((object, item) => {
object[item.name] = {[item.subname]: [item.symbolname]}
},{})
However, I encountered an issue where it only returned one subname. Any suggestions on how to resolve this? Thank you for your help.