1

I want to loop through some data which is in JSON.

{
    "data": [{
        "id": "1561",
        "companyName": "Keeley Furniture Limited",
        "lat": "51.50622110",
        "lng": "-0.10131790",
        "city": "London",
        "street1": "123 Waldorf St",
        "street2": null,
        "street3": "",
        "zip": "SE1 0SW",
        "country": "United Kingdom",
        "email": "[email protected]",
        "profile": "http:\/\/craft.fira\/directory\/company\/1561\/keeley-furniture-limited",
        "businessCategories": []
    }, {
        "id": "1501", ...etc

So far I have this:

$(function() {
     var $members = $('#memberlist');

     $.getJSON("/directory/JSON/fullmembers.json", function(memberlist) {
         console.log('success');
         var members = memberlist.data;
         $.each(members, function(member) {
             $members.append("<li>" + member["companyName"] + "</li>");
         });
    });
});

Which just results in lots of <li>undefined</li> being appended to the ul. Any help would be appreciated!

1 Answer 1

2

The first argument in the each() handler function is the index of the current element in the array. The element itself is the second parameter. Try this:

$.getJSON("/directory/JSON/fullmembers.json", function(memberlist) {
     console.log('success');
     var members = memberlist.data;
     $.each(members, function(index, member) { // note the 'index' param here
         $members.append("<li>" + member["companyName"] + "</li>");
     });
});

Working example

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

2 Comments

That did it Rory, thank you so much! I'll mark as correct when I can. Much appreciated. Have a great day :-)
No problem, glad to help.

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.