0

I have a object like this.

sliderArray: 
[
  "../assets/slides/1.jpg",
  "../assets/slides/2.jpg",
]

Can I reformat it like this in Vue?

sliderArray2: 
[
  {url: require("../assets/slides/1.jpg")},
  {url: require("../assets/slides/2.jpg")},
]
1
  • 1
    Yes you can. Use Array.prototype.map. Commented Mar 27, 2020 at 22:08

1 Answer 1

1

This is easy to do with a map, which applies the same function to every element of an array

let obj = { sliderArray: [
    "../assets/slides/1.jpg",
    "../assets/slides/2.jpg",
  ]
};

function formatArray(a) {
  return a.map(x => { return { url: require(x) } });
}

obj.sliderArray = formatArray(obj.sliderArray);
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.