简体   繁体   English

带有条件的jQuery json响应迭代

[英]Jquery json response iteration with condition

I have this json response... 我有这个json响应...

{
"albums": {
  "data": [
     {
        "created_time": "2011-12-29T11:29:03+0000",
        "id": "10150573478667193",
        "name": "Timeline Photos",
        "photos": {
           "data": [
              {
                 "created_time": "2014-09-04T06:45:15+0000",
                 "source": "https://link.jpg",
                 "id": "10152743142372193",
                 "likes": {
                    "data": [
                       {
                          "id": "10202250935623343",
                          "name": "Name LastName"
                       }
                    ],
                    "summary": {
                       "total_count": 13
                    }
                 }
              }
           ]
        }
     }
  ]
  }
 }
}

The actual response has a lot more album objects and photos objects but i shortened it for showing...Now what i am trying to do is iterate throught response and get the "source" link for the photo that has the highest total_count(most liked photo) in a couple of albums.I managed to get the highest total_count but i cant get the "source" link.Here is how i got highest total_count- 实际的响应有更多的相册对象和照片对象,但是我为了显示而将其缩短了...现在,我要尝试的是遍历整个响应并获取具有total_count最高的照片的“源”链接(最喜欢照片)在几个专辑中。我设法获得了最高的total_count,但我无法获得“源”链接。以下是我获得最高的total_count-

var array=[];
$.each(response.albums.data,function(index,obj){
            //console.log("index1:" + index + " object1:" +obj);
            $.each(obj,function(index2,obj2){
            //console.log("index2:" + index2 + " object2:" +obj2);
            if(typeof obj2 == 'object'){
                $.each(obj2.data,function(index3,obj3){
                //console.log("index3:" + index3 + " object3:" +obj3);
                        $.each(obj3,function(index4,obj4){
                        //console.log("index4:" + index4 + " object4:" +obj4);
                            if(typeof obj4 == 'object'){
                                $.each(obj4,function(index5,obj5){
                                //console.log("index5:" + index5 + " object5:" +obj5);
                                    if(index5 == 'summary'){
                                        $.each(obj5,function(index6,obj6){
                                            //console.log("index6:" + index6 + " object6:" +obj6);
                                            array[array.length] = obj6; 



                                        });

                                    }
                                });
                            }
                        });         
                });
            }
            }); 
        });
        var mostLikedNumber=Math.max.apply(Math,array);
        console.log("Most liked - " +mostLikedNumber);

Any help is appreciated! 任何帮助表示赞赏!

It seems your script is overly complicated: 您的脚本似乎过于复杂:

var mostLikes = -1;
var mostLiked = [];

$.each(response.albums.data, function (idx, album) {
    $.each(album.photos.data, function (idx, photo) {
        if (photo.likes.summary.total_count > mostLikes) {
            mostLikes = photo.likes.summary.total_count;
            mostLiked = [photo.source];
        } else if (photo.likes.summary.total_count == mostLikes) {
            mostLiked.push(photo.source);
        }
    });
});

console.log("Most liked - " + mostLiked.join(",") + " = " + mostLikes);

This accounts for ties as well. 这也说明了联系。

Demo: http://jsfiddle.net/jtbowden/dc84k3x0/ 演示: http//jsfiddle.net/jtbowden/dc84k3x0/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM