LosProgramer
LosProgramer

Reputation: 127

Vue.js - how to get specified fields from object via axios reponse data

This is my JSON object

{
    "id": 2,
    "dani": 5,
    "asset": {
        "id": 2,
        "code": "1",
        "name": "asset1",
        "descr": null,
        "version": "2020-09-01T15:39:53.821+0000",
        "model": {
            "id": 3,
            "code": "4",
            "name": "ZXc43V",
            "descr": "",
            "version": "2020-09-01T15:22:36.953+0000",
            "year": "2020-03-26"
        },
        "location": {
            "id": 1,
            "code": "4",
            "name": "Lokacija 5",
            "descr": "",
            "version": "2020-06-03T12:22:55.693+0000",
            "adress": "Obilicev venac 24",
            "dateFrom": "2020-06-03",
            "dateTo": "2022-05-07",
            "active": false
        }
    }
}

Let's say I have declared array "assets[]". What I want to do with it is that when I use

axios.get('url').then(response => this.array = response.data 

I want to extract from my object (which response.data provides) name of asset (asset.name) and put only name of assets in my array "assets[]", so it contains only asset names from JSON objects, and nothing else.

Thanks

Upvotes: 1

Views: 1441

Answers (2)

Darlan Dieterich
Darlan Dieterich

Reputation: 2537

you can iterate the object and push into the array.

axios.get('url').then((response) => {
  response.data.forEach((item) => {
     this.assets.push(item.asset.name)
  })
})

Upvotes: 0

Boussadjra Brahim
Boussadjra Brahim

Reputation: 1

You could use map function as follows :

this.assets=response.data.map(item=>({name:item.asset.name}));

or if you don't need the field :

this.assets=response.data.map(item=>item.asset.name);

Upvotes: 1

Related Questions