简体   繁体   中英

Write response from multiple data JSON Array

As an example, if I type sicans , I have a response from the API like this:

{
  "data": {
    "Data1": [{
      "name": "sicans",
      "phone_number": "01234",
      "email": "i@a.a",
      "status": "ACTIVE",
    }],
    "Data2": [],
    "Data3": [],
    "Data4": []
  }
}

I need to capture the data and then write the response. I have tried console.log like below, but the it is empty. Do you know how I can get name response from the response?

$.ajax({
  url: api_url + 'search?keyword=' + pencarian,
  type: 'GET',
  success: function(response) {
    for (var i = 0; i < response.data.length; i++) {
      var name = response.data[i].Data1.name;
      console.log(name)
    }
  }
})

Thank you

Try something like this, you need to apply length on the array not on object

  success: function(response) {
    let array = response.data.Data1
    for (var i = 0; i < array.length; i++) {
      var name = array[i].name;
      console.log(name)
    }
  }

response.data is of type Object . So if you write response.data[i] , it returns undefined since response.data has no integer keys.

 var response = { "data": { "Data1": [{ "name": "sicans", "phone_number": "01234", "email": "i@aa", "status": "ACTIVE", }], "Data2": [{ "name": "sicans2", "phone_number": "01234", "email": "i@aa", "status": "ACTIVE", }], "Data3": [{ "name": "sicans3", "phone_number": "01234", "email": "i@aa", "status": "ACTIVE", }], "Data4": [{ "name": "sicans4", "phone_number": "01234", "email": "i@aa", "status": "ACTIVE", }] } } //Trying to access with integer key. Will return "undefined" console.log(response.data[0]); // This example is using "Object.keys()" console.log('With Object.keys()'); Object.keys(response.data).forEach((key) => { var name = response.data[key][0].name; // ^^ this is here because Data1, Data2 etc. are of type Array. We need to get first element console.log(key, name) }); // This example is using "Object.values()" console.log('With Object.values()'); Object.values(response.data).forEach((data) => { var name = data[0].name; // ^^ this is here because Data1, Data2 etc. are of type Array. We need to get first element console.log(name) }); 

If you need to loop from Data1 to Data4 I, what you are looking for is Object.keys

 var response = { "data": { "Data1": [{ "name": "sicans", "phone_number": "01234", "email": "i@aa", "status": "ACTIVE", }], "Data2": [], "Data3": [], "Data4": [] } } var keys = Object.keys(response.data); for (var i = 0; i < keys.length; i++) { var data = response.data[keys[i]] for (var j = 0; j < data.length; j++) { var name = data[j].name; console.log(name); } } 

Otherwise like Eugene Sunic pointed out, you can change the original loop to the following:

  var response = { "data": { "Data1": [{ "name": "sicans", "phone_number": "01234", "email": "i@aa", "status": "ACTIVE", }], "Data2": [], "Data3": [], "Data4": [] } } for (var i = 0; i < response.data.Data1.length; i++) { var name = response.data.Data1[i].name; console.log(name); } 

Let me know if it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM