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?

3
  • Does this answer your question? How to use JSONP on fetch/axios cross-site requests Commented Jan 28, 2021 at 7:39
  • Have you tried something? Have you done some research? This is not difficult. We will not write the code for you Commented Jan 28, 2021 at 7:39
  • That's a JSONP response. Check the duplicate for solutions. Commented Jan 28, 2021 at 7:39

1 Answer 1

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);
});

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.