我想遍历一个 json 对象并计算不同数组中具有相同值的元素。这是我的对象的示例
var testconn =
{"_total": 3,
"values":
[
{
"articlesRead": [
{ "articleId": 1001 },
{ "articleId": 1002 },
{ "articleId": 1003 },
{ "articleId": 1004 },
{ "articleId": 1005 },
{ "articleId": 1006 }
]
},
{
"articlesRead": [
{ "articleId": 1001 },
{ "articleId": 1002 },
{ "articleId": 2001 },
{ "articleId": 2002 },
{ "articleId": 2003 },
{ "articleId": 2004 }
]
},
{
"articlesRead": [
{ "articleId": 1001 },
{ "articleId": 3001 },
{ "articleId": 3002 },
{ "articleId": 3003 },
{ "articleId": 3004 },
{ "articleId": 3005 }
]
}
]
}
我想计算数组中存在多少个 ID。所以对于这个例子,我应该有: 1001 x 3 1002 x 2 1003 x 1 1004 x 1 2001 x 1 等
我正在像这样循环我的对象
for (i = 0; i < testconn.values.length; i++) {
var c = testconn.values[i];
for (j = 0; j < c.articlesRead.length; j++) {
var a = c.articlesRead[j];
for (var key in a) {
if (a[key] is bigger then 1) {
count a[key]
}
return number of
}
}
}