2

How will I add two or three fields from one row in a single array element in Vue? Vue

Vehicles: [
   "BMW 100 1994",
   "Audi 300 2001",
   "Toyota 200 2000",
   "Mazda 104 2011",
   "Car 104 2014",
   "Car2 140 2015" ]

Response

axios.get('/api/manage/manage/vehicles').then(response => {
        this.vehicles = response.data.vehicles;
      })

Database

enter image description here

4
  • It makes no sense to limit yourself like that. Vue is perfectly capable of working with an array of objects. Commented Jun 12, 2018 at 9:59
  • Actually i am adding this autocomplete from buefy. I am new to VueJs. So please if you eplain to me. Thanks Commented Jun 12, 2018 at 10:01
  • buefy.github.io/#/documentation/autocomplete Commented Jun 12, 2018 at 10:02
  • aren't data supports array of objects? data Options / suggestions Array<String>, Array<Number>, Array<Object> Commented Jun 12, 2018 at 10:16

1 Answer 1

1

As commented already vue can handle arrays, or any other type as data.

But to answer your question, assuming that vehicles data from server is an object like:

console.log(response.data.vehicles)
[
  {id: 1, make: 'BMW', model: '100', year: 1994},
  {id: 2, make: 'Audi', model: '1 Series M', year: 1994}
]

To resolve this to your expectations you could do:

axios.get('/api/manage/manage/vehicles').then(response => {
  this.vehicles = response.data.vehicles.map(car => `${car.make} ${car.model} ${car.year}`);
})

See the above snippet to test how this is done:

const vehicles = [{
    id: 1,
    make: 'BMW',
    model: '100',
    year: 1994
  },
  {
    id: 2,
    make: 'Audi',
    model: '1 Series M',
    year: 1994
  }
]

const output = vehicles.map(car => `${car.make} ${car.model} ${car.year}`)
console.log(output);

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.