2

I am trying to access the following data in Vue.js

{"id":1,"name":"Westbrook","created_at":"2017-06-10T16:03:07.336Z","updated_at":"2017-06-10T16:03:07.336Z","stats":[{"id":1,"player_id":1,"points":2558,"assists":840,"rebounds":864,"created_at":"2017-06-10T16:03:07.373Z","updated_at":"2017-06-10T16:03:07.373Z"}]}

self.player = response.name works. now i need self.point

methods: {
    fetchData: function() {
      var self = this;
      $.get("api/v1/players/1", function(response) {
        console.log(response);
        self.player = response.name;
        self.point = response.stats.points 
      });
    }
  }

I have thus far tried response.stats["points"], response.stats[2], response.stats[ { points } ], response.stats[points]

0

3 Answers 3

4

The stats property in your json holds an array in which the first object has a property named points

So use response.stats[0].points


Keep in mind though that the stats is probably an array for a reason. It might hold more than one objects in the future, so always using the first element [0] might not be a valid approach.

Sign up to request clarification or add additional context in comments.

Comments

2

I think it can help you

var json = JSON.parse(data);
json.stats[0].points

enter image description here

Comments

0
response = {"id":1,"name":"Westbrook","created_at":"2017-06-10T16:03:07.336Z","updated_at":"2017-06-10T16:03:07.336Z","stats":[{"id":1,"player_id":1,"points":2558,"assists":840,"rebounds":864,"created_at":"2017-06-10T16:03:07.373Z","updated_at":"2017-06-10T16:03:07.373Z"}]}

If you want to access the name

console.log(response.name) // Westbrook

If you want to access the stats data which contain list, simply target

let stats=response.stats[0] //By getting the first index in the list

Get the points in stats

console.log(stats.points) // 2588

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.