I need to convert an object type to an array of different objects in order to display several tables in my angular project.
Below is my object:
let myObject = {
internalValue:{city:"Paris", country:"France", pinCode:12345},
originalValue:{city:"Nantes", country:"France", pinCode:34567},
score:{city:10, country:100, pinCode:45}
};
Expected Array
[
[
{detail:"Paris", label:"internalValue"},
{detail:"Nantes", label:"originalValue"},
{detail:10, label:"score"}
],
[
{detail:"France", label:"internalValue"},
{detail:"France", label:"originalValue"},
{detail:100, label:"score"}
],
[
{detail:12345, label:"internalValue"},
{detail:34567, label:"originalValue"},
{detail:45, label:"score"}
]
]
My code
let tableData:any;
tableData = _.transform(myObject, result, value, key)=>{
let retValue:any;
_.forIn(value, (v,k)=> {
let tempArr:Array<any>;
let tempObj:any = {};
tempObj.detail= v;
tempObj.label=key;
tempArr.push(tempObj);
retValue.push(tempArr);
})
result = [...retValue];
return result;
},[]);
Stuck here for the next set of loops.
Lodash isn't really required here. You could do this by using the
Object.keysandObject.valuesto iterate over the properties of the objects.Here you initially get the required keys (
city,country,pinCode) from 1st value in the object and iterate over. Below is an example: