如何计算JSON数据中唯一键值对的数量?

时间:2014-03-26 20:41:39

标签: javascript json sorting ebay

我的函数是以下列格式返回JSON数据:

"searchResult":[
        {
           "@count":"100",
           "item":[
              {
                 "itemId":[
                    "151258132867"
                 ],
                 "title":[
                    "Apple iPhone 4 - clean esn - Black (Verizon) Smartphone"
                 ],
                 "globalId":[
                    "EBAY-US"
                 ],
                 "primaryCategory":[
                    {
                       "categoryId":[
                          "9355"
                       ],
                       "categoryName":[
                          "Cell Phones & Smartphones"
                       ]
                    }
                 ],
                 "galleryURL":[
                    "http:\/\/thumbs4.ebaystatic.com\/m\/mGwHzpyvkph9-nU12sCspxw\/140.jpg"
                 ],
                 "viewItemURL":[
                    "http:\/\/www.ebay.com\/itm\/Apple-iPhone-4-clean-esn-Black-Verizon-Smartphone-\/151258132867?pt=Cell_Phones"
                 ],
                 "productId":[
                    {
                       "@type":"ReferenceID",
                       "__value__":"101787954"
                    }
                 ],
                 "paymentMethod":[
                    "PayPal"
                 ],
                 "autoPay":[
                    "false"
                 ],
                 "postalCode":[
                    "93905"
                 ],
                 "location":[
                    "Salinas,CA,USA"
                 ],
                 "country":[
                    "US"
                 ],
                 "shippingInfo":[
                    {
                       "shippingServiceCost":[
                          {
                             "@currencyId":"USD",
                             "__value__":"0.0"
                          }
                       ],
                       "shippingType":[
                          "Free"
                       ],
                       "shipToLocations":[
                          "US"
                       ],
                       "expeditedShipping":[
                          "true"
                       ],
                       "oneDayShippingAvailable":[
                          "false"
                       ],
                       "handlingTime":[
                          "2"
                       ]
                    }
                 ],
                 "sellingStatus":[
                    {
                       "currentPrice":[
                          {
                             "@currencyId":"USD",
                             "__value__":"96.0"
                          }
                       ],
                       "convertedCurrentPrice":[
                          {
                             "@currencyId":"USD",
                             "__value__":"96.0"
                          }
                       ],
                       "bidCount":[
                          "17"
                       ],
                       "sellingState":[
                          "Active"
                       ],
                       "timeLeft":[
                          "P0DT0H0M11S"
                       ]
                    }
                 ],
                 "listingInfo":[
                    {
                       "bestOfferEnabled":[
                          "false"
                       ],
                       "buyItNowAvailable":[
                          "false"
                       ],
                       "startTime":[
                          "2014-03-19T18:29:51.000Z"
                       ],
                       "endTime":[
                          "2014-03-26T18:29:51.000Z"
                       ],
                       "listingType":[
                          "Auction"
                       ],
                       "gift":[
                          "false"
                       ]
                    }
                 ],
                 "returnsAccepted":[
                    "false"
                 ],
                 "condition":[
                    {
                       "conditionId":[
                          "3000"
                       ],
                       "conditionDisplayName":[
                          "Used"
                       ]
                    }
                 ],
                 "isMultiVariationListing":[
                    "false"
                 ],
                 "topRatedListing":[
                    "false"
                 ]
              },
              {
                 "itemId":[
                    "360810145746"
                 ],
                 "title":[
                    "U Apple iPhone 3GS - 8GB - Black (FACTORY UNLOCKED) Smartphone (C)"
                 ],
                 "globalId":[
                    "EBAY-US"
                 ],
                 "subtitle":[
                    "****USA SELLER*****FAST SHIPPING*****TOP SELLER****"
                 ],
                 "primaryCategory":[
                    {
                       "categoryId":[
                          "9355"
                       ],
                       "categoryName":[
                          "Cell Phones & Smartphones"
                       ]
                    }
                 ],
                 "galleryURL":[
                    "http:\/\/thumbs3.ebaystatic.com\/m\/mQGXy5hucEtqk6inUKFu67g\/140.jpg"
                 ],
                 "viewItemURL":[
                    "http:\/\/www.ebay.com\/itm\/U-Apple-iPhone-3GS-8GB-Black-FACTORY-UNLOCKED-Smartphone-C-\/360810145746?pt=Cell_Phones"
                 ],
                 "productId":[
                    {
                       "@type":"ReferenceID",
                       "__value__":"99984545"
                    }
                 ],
                 "paymentMethod":[
                    "PayPal"
                 ],
                 "autoPay":[
                    "true"
                 ],
                 "postalCode":[
                    "10001"
                 ],
                 "location":[
                    "New York,NY,USA"
                 ],
                 "country":[
                    "US"
                 ],
                 "shippingInfo":[
                    {
                       "shippingServiceCost":[
                          {
                             "@currencyId":"USD",
                             "__value__":"0.0"
                          }
                       ],
                       "shippingType":[
                          "Free"
                       ],
                       "shipToLocations":[
                          "US",
                          "CA",
                          "GB",
                          "AU",
                          "FR",
                          "HK",
                          "MT",
                          "IE",
                          "IL",
                          "NZ",
                          "PH",
                          "SG"
                       ],
                       "expeditedShipping":[
                          "true"
                       ],
                       "oneDayShippingAvailable":[
                          "false"
                       ],
                       "handlingTime":[
                          "1"
                       ]
                    }
                 ],
                 "sellingStatus":[
                    {
                       "currentPrice":[
                          {
                             "@currencyId":"USD",
                             "__value__":"84.95"
                          }
                       ],
                       "convertedCurrentPrice":[
                          {
                             "@currencyId":"USD",
                             "__value__":"84.95"
                          }
                       ],
                       "sellingState":[
                          "Active"
                       ],
                       "timeLeft":[
                          "P12DT0H9M11S"
                       ]
                    }
                 ],
                 "listingInfo":[
                    {
                       "bestOfferEnabled":[
                          "false"
                       ],
                       "buyItNowAvailable":[
                          "false"
                       ],
                       "startTime":[
                          "2013-12-08T18:33:51.000Z"
                       ],
                       "endTime":[
                          "2014-04-07T18:38:51.000Z"
                       ],
                       "listingType":[
                          "StoreInventory"
                       ],
                       "gift":[
                          "false"
                       ]
                    }
                 ],
                 "returnsAccepted":[
                    "true"
                 ],
                 "condition":[
                    {
                       "conditionId":[
                          "3000"
                       ],
                       "conditionDisplayName":[
                          "Used"
                       ]
                    }
                 ],
                 "isMultiVariationListing":[
                    "false"
                 ],
                 "discountPriceInfo":[
                    {
                       "originalRetailPrice":[
                          {
                             "@currencyId":"USD",
                             "__value__":"449.0"
                          }
                       ],
                       "pricingTreatment":[
                          "STP"
                       ],
                       "soldOnEbay":[
                          "false"
                       ],
                       "soldOffEbay":[
                          "false"
                       ]
                    }
                 ],
                 "topRatedListing":[
                    "true"
                 ]
              }

等。每个项目属于某个categoryId。例如,在这种情况下,我想要做的是计算每个唯一categoryId显示的次数,然后将前两个categoryId存储在一个数组中。我认为这可以通过For循环完成,但我不确定如何设置。任何帮助表示赞赏,谢谢!

2 个答案:

答案 0 :(得分:0)

循环浏览searchResult.item并将categoryId存储在<CategoryID, NumOccurences>

的哈希值中
var categoryIds = {};
for (var i = 0, l = searchResult.item.length; i < l; i++) {
    var categoryId = searchResult.item[i].primaryCategory.categoryId;
    categoryIds[categoryId] = ++(categoryIds[categoryId] || 0);
}

// console.log(categoryIds);
// > { '1234': 1, '2345': 2, ... }

答案 1 :(得分:0)

这应该有效:

var o = {};
data.searchResult[0].item.forEach(function(item) {
    var id = item.primaryCategory[0].categoryId;
    if (o[id]) o[id]++;
    else o[id] = 1;
});
var top2 = Object.keys(o).sort(function(a, b) { return o[b]-o[a]; }).slice(0, 2);
console.log('Top two categories: ' + top2.join(', '));