6

I have a json array which i fetch by a ajax call and would like to loop through it. The array outputs a category titel and some data records within that category. The array is as followed:

{"Travel":[{"title":"Beautiful title 1"},{"title":"Beautiful title 2"},{"title":"Beautiful title 3"}],"Other":[{"title":"Beautiful title 1"}]}

The basic each function like so can't help me.

$.each(data, function(key, value) {
 console.log(value.title);
}

I want to be able to output it with the main category title and under that have the data records shown.

So for example i want it to look like this:

Travel (3 results):

  • Beautiful title 1
  • Beautiful title 2
  • Beautiful title 3
  • List item

Other (1 results):

  • Beautiful title 1

Any help would be greatly appreciated. Thank you.

asked May 12, 2015 at 4:20
1
  • isnt it an object and not an array? or am i missing something here? Commented May 12, 2015 at 4:33

4 Answers 4

11

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>

answered May 12, 2015 at 4:30
1
  • Thank you, that did the trick, changed the code a bit to fit my needs. I've marked your answer as correct answer! Commented May 12, 2015 at 4:38
1

You would need nested .each() as the array contains nested objects.

$.each(data,function(i,object){
 console.log(i +'('+object.length+')')
 $.each(object, function (index, obj) {
 console.log(obj.title);
 });
});

Fiddle

answered May 12, 2015 at 4:33
0
1

Try

$.each(data, function(key, value) {
 $("<ul />", {
 "class": key.toLowerCase(),
 "html": key + " (" + value.length + " results)<br />"
 }).append($.map(value, function(title, i) {
 return $("<li />", {
 "html": Object.keys(title)[0] + ":" + title.title
 })[0].outerHTML
 })).appendTo("body");
});

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) {
 $("<ul />", {
 "class": key.toLowerCase(),
 "html": key + " (" + value.length + " results)<br />"
 }).append($.map(value, function(title, i) {
 return $("<li />", {
 "html": Object.keys(title)[0] + ":" + title.title
 })[0].outerHTML
 })).appendTo("body");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

0

Actually you have a Travel key their

So you can do it like :

$.each(data.Travel,function(key, value){
 console.log(value.title);
}
answered May 12, 2015 at 4:27
1
  • I can't because the category titles are pulled from database, there could be 300 different categories, so i would never know all the titles otherwise i would have already done that. Commented May 12, 2015 at 4:28

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.