0

I have started a small API project and am at an impass and could use some help, please.

The connect is working fine and I can console.log(data) to return what is expected. What I want to do now is seperate the return data. An example of the data is "id":19, "code": 21, "type", zone. I have tried various ways to separate the index and the element, as I've called them, but cannot get it work. Any help would be greatly appreciate.

    $.ajax(connect).done(function(data) {
        $.each([data], function(index, element){
            $('#AJAXresponse').html('<ul>' + index + ' : ' + element +  '</ul>');
        });
   });
5
  • please provide the complete or exact string json that is received from the api response the parenthesis and square brackets around the json string make alot difference Commented Jun 2, 2018 at 18:27
  • {"id":19,"location":"005100","nlc":"0051","name":"ZONE 1*","location_type":"Zone","country":null,"crs":null,"created_at":"2017-12-13 22:32:14","updated_at":"2017-12-13 22:32:14","deleted_at":null} Commented Jun 2, 2018 at 18:37
  • First, if you print '<ul>' + index + ' : ' + element + '</ul>' it wont be nice, as a <li> is missing... Commented Jun 2, 2018 at 18:46
  • The ID which AJAXresponse relates to, is inside a <li> tag. I can get the JSON to display in the <ul> tags, the problem is, I want more control over the data. I don't want all of it displayed and what I want displaying will be in a different order to how its received. Hope that makes sense :) Commented Jun 2, 2018 at 18:52
  • Try with array.sort w3schools.com/jsref/jsref_sort.asp Commented Jun 2, 2018 at 18:57

1 Answer 1

1

Several problems.

First don't wrap data in []. If you pass an object to $.each it will iterate through all the keys and if you pass an array it will iterate the array

Then don't use html() in the loop as it will replace existing elements each iteration of the loop leaving you with only the last one

Finally you can't put text directly in a <ul> without using <li>

Try

$.ajax(connect).done(function(data) {
  var $ul = $('<ul></ul>');
  $.each(data, function(key, value) {
    $ul.append('<li>' + key + ' : ' + value + '</li>');
  });
  $('#AJAXresponse').html($ul);

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

3 Comments

Amazing! I spent hours researching to try and get it to work, thank you! Would I be able to improve on this and add each of the $.each to a variable so I can access them later? A bit like indexing an array so I can pick what I want displaying?
No need to add them to a variable can access each property of an object any time when you know the property name
This could land me my first Junior Dev role so again, thank you :) :)

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.