Javascript 我能数一数一个名字在JSON中的特定轨迹中出现了多少次吗?

Javascript 我能数一数一个名字在JSON中的特定轨迹中出现了多少次吗?,javascript,jquery,arrays,json,count,Javascript,Jquery,Arrays,Json,Count,在过去的两个月里,我一直在尝试编写脚本!大量的研究!因为我在这方面完全是新手,我想知道你是否能帮助我 看,我刚刚完成了一个JSON(一个大的),这接近我在这里所说的 [{ "League": "English Championship", "Champion": "Team 4", "Vice": "Team 3", "Third": "Team 1", }, { "League": "European Championship", "Champion": "Team 3", "Vice": "

在过去的两个月里,我一直在尝试编写脚本!大量的研究!因为我在这方面完全是新手,我想知道你是否能帮助我

看,我刚刚完成了一个JSON(一个大的),这接近我在这里所说的

[{
"League": "English Championship",
"Champion": "Team 4",
"Vice": "Team 3",
"Third": "Team 1",
}, {
"League": "European Championship",
"Champion": "Team 3",
"Vice": "Team 2",
"Third": "Team 1
}]
我想计算特定团队在元素“冠军”、“副”和“第三名”中出现的次数,因此我可以创建如下内容:

______|_1st_|_2nd_|_3rd_
Team 1|__0__|__0__|__2__|
Team 2|__0__|__1__|__0__|
Team 3|__1__|__1__|__0__|
Team 4|__1__|__0__|__0__|
在学习JS和jQuery时,我想知道是否有可能为这个表开发一个新的数组。如果真的很困难,我可以使用excel在再次放入JSON之前对所有内容进行列表

我只是想要一个开始的小费


感谢您可以在
循环中为..使用
Array.prototype.reduce()
。注意,
“第三组”缺少结束语
:“第1组”

var数据=[{
“联赛”:“英语锦标赛”,
“冠军”:“第四队”,
“副主席”:“第三小组”,
“第三队”:“第一队”,
}, {
“联赛”:“欧洲锦标赛”,
“冠军”:“第三队”,
“副主席”:“第二小组”,
“第三组”:“第1组”
}];
var res=数据减少(功能(obj、b、索引){
如果(!index){//if`index`等于`0`
对于(b中的var prop){//迭代`data`数组中`b`对象的属性
如果(prop!==“League”){//exclude`“League”`来自下一个区块
//将第三个参数处的`obj`:`{}`设置为`reduce()`
//属性设置为'b[prop]`,值设置为'1`
obj[prop]={[b[prop]]:1};
}
}
}否则{
for(obj中的var prop){//迭代`obj`对象的属性
if(obj[prop][b[prop]]){//if`obj[prop][b[prop]]`属性已定义
++obj[prop][b[prop]];//增量值
}
}
};
return obj//return`obj`
}, {});

控制台日志(res)
您可以使用
Array.prototype.reduce()
for..in
循环。注意,
“第三组”缺少结束语
:“第1组”

var数据=[{
“联赛”:“英语锦标赛”,
“冠军”:“第四队”,
“副主席”:“第三小组”,
“第三队”:“第一队”,
}, {
“联赛”:“欧洲锦标赛”,
“冠军”:“第三队”,
“副主席”:“第二小组”,
“第三组”:“第1组”
}];
var res=数据减少(功能(obj、b、索引){
如果(!index){//if`index`等于`0`
对于(b中的var prop){//迭代`data`数组中`b`对象的属性
如果(prop!==“League”){//exclude`“League”`来自下一个区块
//将第三个参数处的`obj`:`{}`设置为`reduce()`
//属性设置为'b[prop]`,值设置为'1`
obj[prop]={[b[prop]]:1};
}
}
}否则{
for(obj中的var prop){//迭代`obj`对象的属性
if(obj[prop][b[prop]]){//if`obj[prop][b[prop]]`属性已定义
++obj[prop][b[prop]];//增量值
}
}
};
return obj//return`obj`
}, {});
控制台日志(res)您可以使用

数据=[{
“联赛”:“英语锦标赛”,
“冠军”:“第四队”,
“副主席”:“第三小组”,
“第三队”:“第一队”,
}, {
“联赛”:“欧洲锦标赛”,
“冠军”:“第三队”,
“副主席”:“第二小组”,
“第三组”:“第1组”
}]
功能getCount(职位、团队){
返回数据。过滤器(函数(x){
返回x[位置]==团队
}).长度
}
日志(getCount('Champion','Team 1'))
日志(getCount('Vice','Team 1'))
log(getCount('Third','Team 1'))
log(getCount('Champion','Team 2'))
log(getCount('Vice','team2'))
log(getCount('Third','team2'))
日志(getCount('Champion','Team 3'))
日志(getCount('Vice','Team 3'))
log(getCount('Third','Team 3'))
您可以使用

数据=[{
“联赛”:“英语锦标赛”,
“冠军”:“第四队”,
“副主席”:“第三小组”,
“第三队”:“第一队”,
}, {
“联赛”:“欧洲锦标赛”,
“冠军”:“第三队”,
“副主席”:“第二小组”,
“第三组”:“第1组”
}]
功能getCount(职位、团队){
返回数据。过滤器(函数(x){
返回x[位置]==团队
}).长度
}
日志(getCount('Champion','Team 1'))
日志(getCount('Vice','Team 1'))
log(getCount('Third','Team 1'))
log(getCount('Champion','Team 2'))
log(getCount('Vice','team2'))
log(getCount('Third','team2'))
日志(getCount('Champion','Team 3'))
日志(getCount('Vice','Team 3'))
log(getCount('Third','Team 3'))
您可以使用它来获取所需的数据结构

const json=[{
“联赛”:“英语锦标赛”,
“冠军”:“第四队”,
“副主席”:“第三小组”,
“第三组”:“第1组”
}, {
“联赛”:“欧洲锦标赛”,
“冠军”:“第三队”,
“副主席”:“第二小组”,
“第三组”:“第1组”
}];
const sum=json.reduce(函数(下一步,res){
常量initObj=()=>({
“冠军”:0,
“副”:0,
“第三”:0
});
如果(!next[res[“Champion”]]){
next[res[“Champion”]=initObj();
}
如果(!next[res[“Vice”]){
next[res[“Vice”]=initObj();
}
如果(!next[res[“Third”]]){
next[res[“Third”]=initObj();
}
下一个[res[“Champion”][“Champion”]+=1;
下一个[res[“Vice”][“Vice”]+=1;
下一个[res[“Third”][“Third”]+=1;
下一步返回;
}, {});
控制台日志(总和)您可以使用来获取所需的数据结构

const json=[{
“联赛”:“英语锦标赛”,
“冠军”:“第四队”,
“副主席”:“第三小组”,
“第三组”:“第1组”
}, {
“联赛”:“欧洲锦标赛”,
“冠军”:“第三队”,
“副主席”:“第二小组”,
“第三组”:“第1组”
}];
const sum=json.reduce(函数(下一步,res){
常量initObj=()=>({
“冠军”:0,
“副”:0,
“第三”:0
});
如果(!next[res[“Champion”]]){
next[res[“Champion”]=initObj();
}
如果(!next[res[“Vice”]){
next[res[“Vice”]=initObj();
}
如果(!next[res[“Third”]]){
next[res[“Third”]=initObj();
}
下一个[res[“Champion”][“Champion”]+=1;