0

I have the following code, which is a restful json api.

// Program guide Rest
$(document).ready(function() {
 $.ajax({
 cache: false,
 url: "http://engridmedia.com/next/api/epg/scheduler/id/200",
 type: 'GET',
 crossDomain: true,
 dataType: 'json',
 success: function() {
 alert("EPG Success");
 },
 error: function() {
 alert('EPG Failed!');
 },
})
.then(function(data) {
 for (var i = 0; i < 6; ++i)
 {
 var prefix = (i == 0 ? "" : i.toString());
 $('.programtitle' + prefix).append(data[i].program_title);
 $('.ch-start' + prefix).append(data[i].start_time);
 $('.ch-end' + prefix).append(data[i].end_time);
 $('.ch-programdesc' + prefix).append(data[i].desc);
 }
});

I want to use the data in my phonegap html . that means i would have to call each data by doing this a bunch of times since i will have over 20 data in the array and sometimes less. is there a for each loop in javascript to do this easily like echoing data from the database in php?

 <?php foreach ($query as $row)
{ ;?>
 <h3><?php echo $row -> program_title; ?> | <?php echo $row -> start_time; ?> | <?php echo $row -> end_time; ?></h3>
 <div>
 <p>
 <?php echo $row -> desc; ?>
 </p>
 </div>
 <?php } ?>
asked Jul 12, 2015 at 23:15
1

1 Answer 1

1

As ja408 sayed you should use this syntax in javascript :

for(var k in yourJson) {
 console.log(k, result[k]);
}

Thanks

answered Jul 13, 2015 at 10:51
Sign up to request clarification or add additional context in comments.

2 Comments

but why do i have to use console log . i thought that only print values in the console of the browser? sorry am not too familiar with javascript. i thought document was the way to print it inside the page?
you dont need the console.log you shold remove it becase old ie dont support this function.

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.