0

我有这个收藏: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
          }
       ]
    }
4

2 回答 2

1

如果您的意思是data数组中的项目数,那就是data.length.

于 2012-09-23T18:22:46.547 回答
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;
};
于 2012-09-23T18:46:03.990 回答