如何使用javascript获取此集合的总计数ID?

时间:2012-09-23 18:21:44

标签: javascript arrays collections

我有这个集合:https://graph.facebook.com/2playcz/albums

此集合包含8个ID。如何使用javascript获取此集合的总计数ID? (总计= 8

来源:

  {
       "data": [
          {
             "id": "201936779932071",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Tr\u00e9ninky",
             "link": "https://www.facebook.com/album.php?fbid=201936779932071&id=190320081093741&aid=41883",
             "cover_photo": "201937046598711",
             "count": 8,
             "type": "normal",
             "created_time": "2012-07-02T09:33:43+0000",
             "updated_time": "2012-09-15T12:05:44+0000",
             "can_upload": false,
             "likes": {
                "data": [
                   {
                      "id": "1788805921",
                      "name": "Edita Nov\u00e1"
                   },
                   {
                      "id": "100001449904219",
                      "name": "Mirka Brani\u0161ov\u00e1"
                   }
                ],
                "paging": {
                   "next": "https://graph.facebook.com/201936779932071/likes?limit=25&offset=25&__after_id=100001449904219"
                }
             }
          },
          {
             "id": "205206429605106",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Turnaje a akce",
             "link": "https://www.facebook.com/album.php?fbid=205206429605106&id=190320081093741&aid=42900",
             "cover_photo": "205208716271544",
             "count": 14,
             "type": "normal",
             "created_time": "2012-07-10T19:36:53+0000",
             "updated_time": "2012-09-15T12:04:05+0000",
             "can_upload": false
          },
          {
             "id": "221784994613916",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Tenisova \u0161kolka 2play",
             "description": "Tenisov\u00e1 \u0161kolka 2play",
             "link": "https://www.facebook.com/album.php?fbid=221784994613916&id=190320081093741&aid=49379",
             "cover_photo": "221785024613913",
             "count": 9,
             "type": "normal",
             "created_time": "2012-08-31T11:19:59+0000",
             "updated_time": "2012-09-14T15:17:53+0000",
             "can_upload": false
          },
          {
             "id": "203405996451816",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Webov\u00e9 fotografie - Logo",
             "link": "https://www.facebook.com/album.php?fbid=203405996451816&id=190320081093741&aid=42285",
             "cover_photo": "203406586451757",
             "count": 11,
             "type": "normal",
             "created_time": "2012-07-05T10:12:40+0000",
             "updated_time": "2012-09-14T15:16:40+0000",
             "can_upload": false
          },
          {
             "id": "190332361092513",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Cover Photos",
             "link": "https://www.facebook.com/album.php?fbid=190332361092513&id=190320081093741&aid=39232",
             "cover_photo": "225939404198475",
             "count": 2,
             "type": "normal",
             "created_time": "2012-06-09T13:52:38+0000",
             "updated_time": "2012-09-12T18:15:51+0000",
             "can_upload": false
          },
          {
             "id": "190802884378794",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Wall Photos",
             "link": "https://www.facebook.com/album.php?fbid=190802884378794&id=190320081093741&aid=39324",
             "cover_photo": "190802891045460",
             "count": 2,
             "type": "wall",
             "created_time": "2012-06-10T13:19:48+0000",
             "updated_time": "2012-07-17T17:16:19+0000",
             "can_upload": false
          },
          {
             "id": "205207126271703",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Ostatn\u00ed",
             "link": "https://www.facebook.com/album.php?fbid=205207126271703&id=190320081093741&aid=42902",
             "cover_photo": "205209679604781",
             "count": 4,
             "type": "normal",
             "created_time": "2012-07-10T19:40:05+0000",
             "updated_time": "2012-07-16T14:47:16+0000",
             "can_upload": false,
             "likes": {
                "data": [
                   {
                      "id": "100001449904219",
                      "name": "Mirka Brani\u0161ov\u00e1"
                   }
                ],
                "paging": {
                   "next": "https://graph.facebook.com/205207126271703/likes?limit=25&offset=25&__after_id=100001449904219"
                }
             }
          },
          {
             "id": "190320914426991",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Profile Pictures",
             "link": "https://www.facebook.com/album.php?fbid=190320914426991&id=190320081093741&aid=39224",
             "cover_photo": "190327474426335",
             "count": 1,
             "type": "profile",
             "created_time": "2012-06-09T13:29:16+0000",
             "updated_time": "2012-06-09T13:43:08+0000",
             "can_upload": false
          },
          {
             "id": "190322704426812",
             "from": {
                "name": "2play.cz - Tenisov\u00e1 \u0161kola",
                "category": "Sports league",
                "id": "190320081093741"
             },
             "name": "Logo",
             "description": "Loga spole\u010dnosti",
             "link": "https://www.facebook.com/album.php?fbid=190322704426812&id=190320081093741&aid=39225",
             "type": "normal",
             "created_time": "2012-06-09T13:34:09+0000",
             "updated_time": "2012-07-05T10:16:58+0000",
             "can_upload": false
          }
       ]
    }

2 个答案:

答案 0 :(得分:1)

如果您指的是data数组中的项目数,则只有data.length

答案 1 :(得分:0)

首先, 9 ID。如果要计算数组中元素的数量,则有一个内置属性length。因此,如果您的对象被调用x,则可以通过访问x.data.length来检索数据的长度。

另一方面,如果你想计算唯一ID的数量(ID应该是唯一的,但如果你真的想要),你必须手动迭代数组并计算它们:

var countIds = function (arr) {
    var uniqueIds = {}, num = 0;
    if (!arr.data) return false;
    arr.data.forEach(function (val, i) {
      if (typeof uniqueIds[val.id] === "undefined") {
        ++num;
        uniqueIds[val.id] = true;
      }
    });
    return num;
};