Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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 })
        )}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading