I have an object like this:
obj1 = {A: {bottom: 63, mid: 4, top: 15}, B: {bottom: 30, mid: 23, top: 5}, C: {bottom: 41, mid: 25, top: 16}}
and I'm trying to convert to something like this:
obj2 =
{group: "A", bottom: "63", mid: "4", top: "15"}
{group: "B", bottom: "30", mid: "23", top: "5"}
{group: "C", bottom: "41", mid: "25", top: "16"}
by following this post but I'm getting an error obj1[key].forEach is not a function any ideas why I'm getting this error?
this is the code I'm using which is basically the same from the original:
obj1 ={A: {bottom: 63, mid: 4, top: 15}, B: {bottom: 30, mid: 23, top: 5}, C: {bottom: 41, mid: 25, top: 16}}
const result = []
// for each key in obj
Object.keys(obj1).forEach(key => {
// for each array element of the property obj[key]
obj1[key].forEach((value, index) => {// in this line I'm getting the error
// if an object doesn't exists at the current index in result
// create it
if (!result[index]) {
result[index] = {}
}
// at the result index, set the key to the current value
result[index][key] = value
})
})
console.log(result)