Javascript 在JSON数组中计数和显示值
我有一些数据如下所示:Javascript 在JSON数组中计数和显示值,javascript,arrays,json,sorting,Javascript,Arrays,Json,Sorting,我有一些数据如下所示: { "data": [ { "tags": [ "design", "development", "code", "foo", "bar" ], "views": [ "81" ] ... }, { "tags": [
{
"data": [
{
"tags": [
"design",
"development",
"code",
"foo",
"bar"
],
"views": [
"81"
]
...
},
{
"tags": [
"design",
"foo",
"photoshop",
"alice",
"bob"
],
"views": [
"28"
]
...
}
foo: 2
design: 2
alice: 1
我希望做的是循环遍历每个数据项,统计标记,然后显示每个标记出现的次数。不过,我不想手动查询每个标记
因此,理想情况下,我希望它显示如下:
{
"data": [
{
"tags": [
"design",
"development",
"code",
"foo",
"bar"
],
"views": [
"81"
]
...
},
{
"tags": [
"design",
"foo",
"photoshop",
"alice",
"bob"
],
"views": [
"28"
]
...
}
foo: 2
design: 2
alice: 1
等等。最终,我希望用Highcharts、Amcharts或类似的东西将其合并到图表中
我管理过的最接近的是这个,取自这个站点上的另一个解决方案(这并没有创建一个完整的显示,但我首先尝试以任何可能的方式掌握计算值本身):
$.getJSON('myarray.json',函数(数据){
var值=0;
对于(var i=0;i
但是这个输出是不正确的。任何帮助都将不胜感激。再一次,我想查看每个数据集的标记,并输出每个数据集发生多少次的计数(理想情况下按降序排列) 您正确地循环了每个项目,但忘记了每个项目中的“标记”也是一个项目列表
$.getJSON('myarray.json', function(data) {
var tagcount = {};
for (var i = 0; i < data.data.length; i++) {
var item = data.data[i];
if( item.tags != undefined ) {
for (var j = 0; j < item.tags.length; j++) {
var tag = item.tags[j];
if( tagcount[tag] == undefined ) {
tagcount[tag] = 1;
} else {
tagcount[tag]++;
}
}
}
}
console.log(tagcount);
});
$.getJSON('myarray.json',函数(数据){
var tagcount={};
对于(var i=0;i
通过对数据进行迭代,您可以得到想要的结果,我以您的数据为例:
var data = {
"data": [{
"tags": [
"design",
"development",
"code",
"foo",
"bar"],
"views": [
"81"]
}, {
"tags": [
"design",
"foo",
"photoshop",
"alice",
"bob"],
"views": [
"28"]
}]
};
然后我们可以这样迭代:
var countArray = [];
$.each(data.data, function(dataKey, dataValue) {
$.each(dataValue.tags, function(tagKey, tagValue) {
if( !(tagValue in countArray) ) {
countArray[tagValue] = 1;
} else {
countArray[tagValue]++;
}
});
});
控制台日志将被输出
console.log(countArray);
[design: 2, development: 1, code: 1, foo: 2, bar: 1…]
JSIDLE:var result={};
对于(变量i=0;i
您必须使用字典计算所有出现的次数,然后将其滚动到一个数组中并对其进行排序(如果您希望得到与输出相同的结果:
// this will count per string each occurrence
var occurrencies = {};
function fetchAndSort(data) {
var i,j;
for (i = 0; i < data.data.length; i++) {
var tags = data.data[i].tags;
// now iterate inside tags
for( j=0; j < tags.length; j++){
// if there's not yet an entry in the dictionary, just create it
if(!occurrencies[tags[j]]){
occurrencies[tags[j]] = 0;
}
occurrencies[tags[j]]++;
}
}
// at this point there's an object with all the keys and the relative count
// if you want an ordered list you have to use an array because iterate on object
// keys in JS [doesn't guarantee any order][1]
var array = [];
for( i in occurrencies ){
array.push({name: i, count: occurrencies[i]});
}
// now sort it
array.sort(function(entry1, entry2){
// sort the array descending
return entry2.count - entry1.count;
});
for( i = 0; i<array.length; i++){
console.log(array[i].name + ':' + array.count);
}
};
$.getJSON('myarray.json', fetchAndSort);
//这将对每次出现的每个字符串计数
变量发生率={};
函数fetchAndSort(数据){
varⅠ,j;
对于(i=0;i 对于(i=0;i你怎么说这个data.data[i]。tags==“foo”
,data.data[i].tags
是一个数组,您正在与一个字符串进行比较?我试图使用这个:谢谢!我不知道为什么,但我得到了一个类型错误:“无法读取未定义的属性'length'”?可能是您的数据数组在格式上与我的示例略有不同。嗯,您可以显示您正在迭代的整个数据结构总之,我猜这与我使用变量名“data”的第一次迭代有关,您可能使用了另一个变量名来分配数据结构。谢谢!我也收到了一个“无法读取属性”长度为undefined的错误——我以前也收到过这个错误,我不确定我是如何使用的。长度不正确。我已更新了代码,以包括检查我们正在检查的项上是否确实存在“标记”。我们的收藏中似乎有一些没有标签的物品。我认为如果有“标签”:[]
会更明智,而不是完全忽略它,但我想这也会起作用。很好,这就是问题所在,非常感谢!我有一些数据是空的/没有标签。
// this will count per string each occurrence
var occurrencies = {};
function fetchAndSort(data) {
var i,j;
for (i = 0; i < data.data.length; i++) {
var tags = data.data[i].tags;
// now iterate inside tags
for( j=0; j < tags.length; j++){
// if there's not yet an entry in the dictionary, just create it
if(!occurrencies[tags[j]]){
occurrencies[tags[j]] = 0;
}
occurrencies[tags[j]]++;
}
}
// at this point there's an object with all the keys and the relative count
// if you want an ordered list you have to use an array because iterate on object
// keys in JS [doesn't guarantee any order][1]
var array = [];
for( i in occurrencies ){
array.push({name: i, count: occurrencies[i]});
}
// now sort it
array.sort(function(entry1, entry2){
// sort the array descending
return entry2.count - entry1.count;
});
for( i = 0; i<array.length; i++){
console.log(array[i].name + ':' + array.count);
}
};
$.getJSON('myarray.json', fetchAndSort);