Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
133 views
in Technique[技术] by (71.8m points)

vue.js - How do I loop JSON and output value?

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?

question from:https://stackoverflow.com/questions/65932774/how-do-i-loop-json-and-output-value

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...