javascript - jQuery loop through JSON array -
i have json array fetch ajax call , loop through it. array outputs category titel , data records within category. array followed:
{"travel":[{"title":"beautiful title 1"},{"title":"beautiful title 2"},{"title":"beautiful title 3"}],"other":[{"title":"beautiful title 1"}]}
the basic each function can't me.
$.each(data, function(key, value) { console.log(value.title); }
i want able output main category title , under have data records shown.
so example want this:
travel (3 results):
- beautiful title 1
- beautiful title 2
- beautiful title 3
- list item
other (1 results):
- beautiful title 1
any appreciated. thank you.
var data = {"travel":[{"title":"beautiful title 1"},{"title":"beautiful title 2"},{"title":"beautiful title 3"}],"other":[{"title":"beautiful title 1"}]}; $.each(data, function (key, value) { $('body').append($('<div></div>').html(key + ' (' + value.length + ' results)')); var list = $('<ul></ul>'); $('body').append(list); $.each(value, function (index, titleobj) { list.append('<li>' + titleobj.title + '</li>'); }); });
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
Comments
Post a Comment