1
votes

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

1
There are illegal characters, which the error states in clear english, and if those are removed, it doesn't throw any error. You further didn't state what the error should be, nor what the code is supposed to do, so i have no clue, what you are talking about.ASDFGerte
For starters, hit "Run code snippet" and see the output -- you have a syntax error newObject[jsonVal[0][j] = d] is not valid.skyline3000
What output are you expecting?Andy

1 Answers

-1
votes

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)