ReactJS: ';' expected while mapping over an array

I am trying to create an array of objects in the following manner:

function allNewDice(){
    const newDice = new Array(10).fill(0).map(x => {
        value: Math.ceil(Math.random() * 6), 
        isOn : false }
    )}

However next to isOn property, I am getting the error ‘;’ expected
What am I doing wrong

If I use a for loop to create the same, it works. However mapping is not working

>Solution :

This has to do with the arrow function syntax. To return an object, you need to wrap it in parentheses.

function allNewDice(){
        const newDice = new Array(10).fill(0).map(x => ({
            value: Math.ceil(Math.random() * 6), 
            isOn : false })
        )}

Leave a Reply