I have an Excel file as following:
[["Country", "City", "Zip"], ["CNTR1", "CT1", 101], ["CNTR1", "CT2", 102], ["CNTR2", "CT3", 103]]
The above is a 2D array interpretation of the 4 rows of the Excel. I want from the above, to get the following object:
{
level1: "Country",
items: [{
"CNTR1": {
level2: "City",
items: [{
"CT1": {
level3: "Zip",
items: [
"101"
]
}
}, {
"CT2": {
level3: "Zip",
items: [
"102"
]
}
}]
}
}, {
"CNTR2": {
level2: "City",
items: [{
"CT3": {
level3: "Zip",
items: [
"103"
]
}
}]
}
}]
}
The main purpose is to have a hierarchy, since the above Excel should have a left to right dependency, because the values will be used in dependent dropdowns (so if we have country "CNTR1", all cities and zip codes should be part of its object, since after selecting "CNTR1" in the countries dropdown, only CT1 and CT2 should show in the cities dropdown, and so on for the zip codes), and based on this dependency, we should have the subvalues for each of the nested levels. Also the above should not be limited to three levels deep, so it should be something with reducers/recursive solutions.
If anyone has a better object schema solution, since the above is what I thought would fit the purpose of the dependent dropdowns while also retrieving the column names, feel free to share. The dropdowns will be dynamic, with dependency based on the left to right columns, and values based on the same "higher" hierarchy value.
Hopefully it is clear enough.
Up until now I have:
items.map(row => row.reduceRight((value, key) => ({[key]: value}))).reduce((acc, value) => (Object.assign(acc, value)))
but this one just gives results of type:
{
"CNTR1": {
"CT2": 102
},
"CNTR2": {
"CT3": 103
}
}
(disregard the loss of nested property data since the merge is not deep) The names of the columns are not included, and it seems sort of poor in data structure for the purpose specified.
Thanks