1

I have this data array holding repetitive item in the component:

data () {
datasets: [ {
        text: "",
        value:[ {
          x: "",
          y: "",
          type: 'bar'
        }]
        },
          {
        text: "",
        value: [{
          x: "",
          y: "",
          type: 'bar'
        }]
        },
        {
        text: "",
        value: [{
          x: "",
          y: "",
          type: 'bar'
        }]
        }
]
}

As you can see those are the same items. Is it possible to set this data like

datasets:[ {
        text: "",
        value:[ {
          x: "",
          y: "",
          type: 'bar'
        }]
        } * 3 ]

Instead of hardcoding it several times?

0

1 Answer 1

3

It is:

datasets: Array(3).fill().map(() => ({
  text: "",
  value:[ {
    x: "",
    y: "",
    type: 'bar'
  }]
}))

In case elements are guaranteed to be read-only and can benefit from holding a reference to the same object, it can be:

datasets: Array(3).fill({
  text: "",
  value:[ {
    x: "",
    y: "",
    type: 'bar'
  }]
})
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.