I want to split a nested Object into an Array of Objects. I am able to achieve the same using two FOR Loops.
Is there any way I can have a better solution, or this is correct?
Below snippet for reference.
var Input = {
"English": {
"v1": "10",
"v2": "11",
"v3": "34"
},
"Hindi": {
"v1": "14",
"v2": "16",
"v3": "18"
}
}
var Output = []
for (let i = 0; i < Object.keys(Input).length; i++) {
for (let j = 0; j<Object.keys(Object.values(Input)[i]).length; j++) {
Output.push({
"parameter": Object.keys(Input)[i],
"machine": Object.keys(Object.values(Input)[i])[j],
"value":Object.values(Object.values(Input)[i])[j]
})
}
}
console.log(Output)
Object.keys(Input)repeatedly - call it once and cache the results for reuse.for (const [key, value] of Object.entries(Input)) {...}.