0

I have some JSON being returned from an api like this:

callback({"Message":"","Names”:[{“id”:”16359506819","Status":"0000000002","IsCurrent":true,"Name":"JAKE","NameType”:”Small,”Postcode”:”2000”,”Score":100,"State”:”NSW”}]})

Vuejs Method

methods: {
    getResults() {
      // sent a GET request
      axios.get("https://myapi" + this.searchvalue +"&maxResults=10&guid=" + this.guidId).then((response) => {
        if(response.status===200){
            if(response.data){
                console.log(response.data);

                // need to console log name for each item
                // console.log("Name :" + response.data.Names.Name)
            }
            else{
                //do nothing 
                }   
        }
        }).catch(function (error){
            console.log(error);
      });
    },
  },
};

How do I loop through each item and console log the name value?

deeej
  • 357
  • 1
  • 6
  • 22
  • Does this answer your question? [How to use JSONP on fetch/axios cross-site requests](https://stackoverflow.com/questions/43471288/how-to-use-jsonp-on-fetch-axios-cross-site-requests) – Robby Cornelissen Jan 28 '21 at 07:39
  • Have you tried something? Have you done some research? This is not difficult. We will not write the code for you – Gert B. Jan 28 '21 at 07:39
  • That's a [JSONP](https://en.wikipedia.org/wiki/JSONP) response. Check the duplicate for solutions. – Robby Cornelissen Jan 28 '21 at 07:39

1 Answers1

1

Names is a list of objects, so you could iterate this list and log the property .Name

const data = {
  Message: "",
  Names: [
    {
      id: "16359506819",
      Status: "0000000002",
      IsCurrent: true,
      Name: "JAKE",
      NameType: "Small",
      Postcode: "2000",
      Score: 100,
      State: "NSW",
    },
    {
      id: "16359506819",
      Status: "0000000002",
      IsCurrent: true,
      Name: "LONG",
      NameType: "Small",
      Postcode: "2000",
      Score: 100,
      State: "NSW",
    },
  ],
};

data.Names.forEach((obj) => {
  console.log(obj.Name);
});
hgb123
  • 13,869
  • 3
  • 20
  • 38