0

I am attempting to set up a short hand in JavaScript and Vue.js for an Array Object with a length of 5, then destructure the values of the array Object into a new array. In my Vue.js project, I added the following "finalArray" property, along with the .keys() property to reassign the object keys as the values passed into each new array object:

const finalArray = ref([...Array(5).keys()])

Instead of returning an array object with the keys assigned as the values, I wish to return an array of objects with each object value represented as an empty string, like so:

[
  {0: ""},
  {1: ""},
  {2: ""},
  {3: ""},
  {4: ""},
]

However, when I remove the keys() property, I get undefined as the value on the key-value pair of each object. How can I set up the Array object (with a length of 5) to return each object with the values represented as empty strings?

JS_is_awesome18
  • 1,587
  • 7
  • 23
  • 67
  • 5
    just map the resulting array `[...Array(5).keys()].map((k) => ({ [k]: '' }))`, or use `Array.from()` which gives you a built in map `Array.from({ length: 5 }, (_, i) => ({ [i]: '' }))` – pilchard Feb 07 '23 at 23:10

0 Answers0