如何计算JSON数据中唯一键值对的数量?
How do I count the number of unique key:value pairs in JSON data?
我的函数以以下格式返回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存储在一个数组中。我认为这可以用For循环来完成,但我不确定它是如何建立的。任何帮助都很感激,谢谢!
应该可以:
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(', '));
遍历searchResult.item
并将categoryId
s存储在<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, ... }
相关文章:
- 正在数组中存储键值对
- 如何将新的键/值对元素添加到现有数组
- 在键值对中对求和值进行下划线
- 返回由键/值对定义的数组
- 在url参数javascript中存储键值对列表
- 将数组转换为键值对
- jQueryAJAX-将额外的键/值对推送到序列化的$_POST数组中
- 使用jQuery从ajax调用访问键值对
- 将动态键值对传递给函数
- d3使用键值对对对多个值的数据进行分组
- JS在封装对象中查找键值对
- 从html标记格式的字符串中提取键值对
- JavaScript 在函数中声明键值对
- 在JavaScript中为表单元素生成键值对
- 如何给$('inputs')每个函数,以将数据存储为 asp.net 中的键值对
- 从 JSON 获取键值对而不使用 .(DOT) 在 Java 脚本或 jquery 中
- 在键值对的唯一组合上合并对象
- 动态插入并从数组中检索键值对 - Javascript
- 如何获取对象中唯一的键值对的值
- 如何计算JSON数据中唯一键值对的数量?