0

I used this vue.js library that requires an array in this format

    autocompleteItems: [
  {
    text: "Css"
  },
  {
    text: "Javascript"
  },
  {
    text: "Java"
  },
  {
    text: "Web Design"
  },
  {
    text: "Spring"
  }
],

However I need to get the items of that array from other array, how do I turn my "normal" array to this required format?

4
  • What you show is a normal array. It just happens to contain objects with a text property. Commented Jul 13, 2020 at 17:29
  • Use a map: newArray = oldArray.map(text => {text}) Commented Jul 13, 2020 at 17:31
  • I didn't know this was called an array of objects, i suppose by normal array I meant array of strings, so like array=['Css','Javascript','Java'] and I need to turn that into an array of obejcts with a text property Commented Jul 13, 2020 at 17:32
  • An array has a length and zero or more elements. No requirement for them to be any specific type. ['one', 2, { value: 'three'}] is a valid array. MDN: Array Commented Jul 13, 2020 at 18:11

1 Answer 1

2

This is an example that will give you an idea of how to add keys to your data

array=["Css","Javascript","Java","Web Design","Spring"]

result=array.map(o => ({text:o}));
console.log(result)

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.