skip to Main Content

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

3

Answers


  1. 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 })
            )}
    
    Login or Signup to reply.
  2. function allNewDice() {
      const newDice = new Array(10).fill(0).map(x => {
        return {
          value: Math.ceil(Math.random() * 6),
          isOn: false
        }
      })
    }
    
    Login or Signup to reply.
  3. function allNewDice() {
        const newDice = new Array(10).fill(0).map((x) => ({
            value: Math.ceil(Math.random() * 6),
            isOn: false,
        }));
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search