How to optimize this transformation from array to object with specific key?
I have this array that inside has other arrays, and I wanted to turn this array into an array of objects. I would like to do this without using this index, and the object would like it to have these specific keys. I'm new to javascript and I would like to know if the way I did it was the best way, or if I can use a Map or Reduce to do what I want.
const listaCesar = [["cesar", "1", 1], ["thiago", "2", 2], ["giuseppe", "3", 3]]
const dict = []
listaCesar.forEach(item => dict.push({name: item[0], id: item[1], age: item[2]}))
console.log(dict)
This code works and gives me the expected result, but I don't know if I did it in the best way
ExpectResult = [{name: "cesar", id: "1", age: "1"}, {name: "thiago", id: "2", age: "2"}, {name: "giuseppe", id: "3", age: "3"}]
.map(item => ({ ... }))instead.map()should yield similar performance. I would also use argument destructuring assignment.const dict = listaCesar.map(([name, id, age]) => ({ name, id, age }))