开发者

How do I do a "for ...in ...." loop in Javascript?

simple_client = new simplegeo.PlacesClient('gHPQAUFbHHL3sHcfSqaetBMskrKZY5');
        var place_query = "starbucks";
        simple_client.search({{ browser_lat }}, {{ browser_long }}, { q: place_query }, function(err, data) {
            if (err) {
                console.error(err);
            } else {
                console.log(JSON.stringify(data));

            }
        });

In this code, I log the entire JSON (in a string format) in the console. However, what if I 开发者_如何学运维want to loop through "data", and log each one?


Like so:

for(var key in data) {
   if(!data.hasOwnProperty(key)) continue;
   //do something with key or data[key]
}


If you are using jQuery you can use $.each.

$.each(data, function(i,v){
    alert(v);
});


If your using underscore.js you can use _.each

_.each(data, function(value, key, data) {
    // do something with value
});
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜