Someone with experience should be able to understand the error with ease, but for me it is hard, I tried learning online about map and for loop but no luck understanding the error, can someone please help me.
let jsonVal = [["ID","Name","Age"],["212","David","38"],["213","Mike","42"]]
let newJsonVal = []
for (let i =1; i< jsonVal.length-1; i ) {
let newObject ={}
jsonVal[i].map((d,j) => {
newObject[jsonVal[0][j] = d]
})
newJsonVal.push(newObject);
console.log(newJsonVal);
}
The output should look something like this:
[
{
"ID": "212",
"Name": "David",
"Age": "38"
},
{
"ID": "213",
"Name": "Mike",
"Age": "42"
}
]
Thanks for your help in advance
CodePudding user response:
As the previous answer but this time with multiple objects:
let jsonVal = [["ID","Name","Age"],["212","David","38"],["213","Mike","42"]]
let [keys, ...objs] = jsonVal;
let newJsonVal = []
objs.map((obj, i)=>{
const newObj = keys.map((key, i)=>[key, obj[i]]);
newJsonVal.push(Object.fromEntries(newObj));
})
console.log(newJsonVal)