【发布时间】:2015-11-18 10:27:55
【问题描述】:
我需要查看代码在海关键中出现的次数,然后在 (name:) 下显示代码以及它在 (data:) 中出现的次数。我想我很接近请看下面的sn-p。因此,当我控制台日志数据时,我只想看到类似 - 名称:123213 数据:22。
// Here is my json object
var json = [
"G": "PIF",
"H": "FOB",
"I": "NINGBO",
"J": "NGB",
"K": "2014-10-01",
"M": "2014-10-01",
"Y": "LIVERPOOL",
"zp": "LIV",
"N": "2014-11-09",
"P": "2014-11-09",
"R": "2014-11-09T12:01",
"V": true,
"zk": " ",
"zo": "7",
"Customs": [
"39210000"
],
"LatLon": {}
},
// ...... etc
// Here is my failed attempt
$(document).ready(function () {
var CommodityCounts = {};
var Commoditycds = [];
var totalCount = 0;
//loop through the object
$.each(json, function(key, val) {
var Commoditycd = val["Customs"];
//build array of unique country names
if ($.inArray(Commoditycd, Commoditycds) == -1) {
Commoditycds.push(Commoditycd);
}
//add or increment a count for the country name
if (typeof CommodityCounts[Commoditycd] == 'undefined') {
CommodityCounts[Commoditycd] = 1;
}
else {
CommodityCounts[Commoditycd]++;
}
//increment the total count so we can calculate %
totalCount++;
});
//console.log(Commoditycds);
var data = [];
//loop through unique countries to build data for chart
$.each(Commoditycds, function(key, Commoditycd) {
data.push({
name: Commoditycd,
data: CommodityCounts
});
});
console.log(data);
});
// Need the data to be show like (name of the code and how many times it appears in my json object- name: '123123', data: [83]
【问题讨论】:
-
name:123213 data:22与您发布的数组有什么关系?我不明白预期的行为。编辑:我认为您希望名称与海关属性相匹配,但仍然不清楚应该是什么数据以及如果数组中有多个对象,您期望什么。您必须使用 MCVE 复制问题来编辑您的问题 -
仅供参考,您拥有的是对象数组,而不是 JSON。
-
customs数组中可以有多个数字吗? -
@RoryMcCrossan 不错的一个
-
你从哪里得到
123213?为什么是data: 22?
标签: javascript jquery json object