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 } ?>
-
Look at "for in" loop - developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/…ja408– ja4082015年07月12日 23:55:18 +00:00Commented Jul 12, 2015 at 23:55
1 Answer 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
Haniel Bitton
841 silver badge7 bronze badges
Sign up to request clarification or add additional context in comments.
2 Comments
Rich fiifi
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?
Haniel Bitton
you dont need the console.log you shold remove it becase old ie dont support this function.
default