Change array Response to object

Asked

Viewed 31 times

0

I’m doing a get and the answer is coming like this:

[
 {"cep":"01050-070",
  "rua":"Rua Álvaro de Carvalho",
  "bairro":"Centro",
  "cidade":"São Paulo",
  "estado":"SP"}
]

I’d like Re sponse to come like this:

{
  "cep": "01050-070",
  "rua":"Rua Álvaro de Carvalho",
  "bairro":"Centro",
  "cidade":"São Paulo",
  "estado":"SP"
}

My method is like this:

searchCep () {
  this.$validator
    .validateAll()
    .then(success => {
      if (success) {
        const url = `http://localhost:4001/${this.cep}`
        axios.get(url)
          .then(response => this.data = response.data)
          .catch(error => console.log(error))
      }
    })

}

1 answer

1

Who generates the answer is the server, if you have no way to change there you only have to deal with what you receive:

if (response.data.length > 0) this.data = response.data[0];

PS: You know that URL http://localhost:4001... will only work in your machine right?

  • Yes I do, I’m just studying. What would be the complete method with this change of yours?

Browser other questions tagged

You are not signed in. Login or sign up in order to post.