I’m trying to Transform Array values using the map method and print out values using the console.
function transformToObjects(numberArray) {
return numberArray.map((number) => {
return { val: number };
});
}
I tried to print out the array values using console.log(numberArray);
but the browser keep showing "ReferenceError".
The expected outcome should look like this:
{val: 1}, {val: 2}, {val: 3}
2
Answers
You’re encountering is that you’re trying to print the numberArray directly after the map operation, but the numberArray itself is not modified by the map method. Instead, the map method returns a new array with the transformed values.
Try this