skip to Main Content

My function is returning JSON data in the following format:

"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"
                 ]
              }

etc. Each item falls under a certain categoryId. So for example in this case, what I want to do is count the number of times each unique categoryId shows up, then store the top two categoryIds in an array. I think this could be done with a For loop, but I’m not sure how that would be set up. Any help is appreciated, thanks!

2

Answers


  1. Loop through searchResult.item and store the categoryIds in a hash of <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, ... }
    
    Login or Signup to reply.
  2. This should work:

    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(', '));
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search