Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/473.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
通过JavaScript或Linq格式化JSON集合字符串_Javascript_Linq - Fatal编程技术网

通过JavaScript或Linq格式化JSON集合字符串

通过JavaScript或Linq格式化JSON集合字符串,javascript,linq,Javascript,Linq,需要帮助通过Javascript或linq格式化我的JSON集合吗 假设在第页上,显示日历日1到5的数据 我的JSON字符串如下所示: [ {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”}, {“studName”:”aaa”,”class”:”5”,”commentsDay”:”4”}, {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”}, {“studName”:”a

需要帮助通过Javascript或linq格式化我的JSON集合吗

假设在第页上,显示日历日1到5的数据

我的JSON字符串如下所示:

[
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”4”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”4”},
    {“studName”:”bbb”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”ccc”,”class”:”6”,”commentsDay”:”3”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”1”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”3”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”2”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”1”} 
]
[
    {“studName”:”aaa”,”class”:”5”,”TotalComments”:”4”,”Details”:”2,0,0,2,0”},
    {“studName”:”bbb”,”class”:”5”,”TotalComments”:”1”,”Details”:”1,0,0,0,0”},
    {“studName”:”ccc”,”class”:”6”,”TotalComments”:”1”,”Details”:”0,0,1,0,0”},
    {“studName”:”ddd”,”class”:”6”,”TotalComments”:”4”,”Details”:”2,1,1,0,0”}
]
Name | Total | 1 | 2 | 3 | 4 | 5 |

Class 5 //displaying all class 5 data 

aaa  |  4    | 2 | 0 | 0 | 2 | 0 |  
bbb  |  1    | 1 | 0 | 0 | 0 | 0 |  

Class 6 //displaying all class 6 data

ccc  |  1    | 0 | 0 | 1 | 0 | 0 |  
ddd  |  4    | 2 | 1 | 1 | 0 | 0 |  
并希望按照如下所示的格式: {“StudentName”、“Class”、“TotalComments”、“CommentDetails”}

因此,我上面提到的集合字符串的格式应如下所示:

[
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”4”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”4”},
    {“studName”:”bbb”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”ccc”,”class”:”6”,”commentsDay”:”3”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”1”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”3”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”2”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”1”} 
]
[
    {“studName”:”aaa”,”class”:”5”,”TotalComments”:”4”,”Details”:”2,0,0,2,0”},
    {“studName”:”bbb”,”class”:”5”,”TotalComments”:”1”,”Details”:”1,0,0,0,0”},
    {“studName”:”ccc”,”class”:”6”,”TotalComments”:”1”,”Details”:”0,0,1,0,0”},
    {“studName”:”ddd”,”class”:”6”,”TotalComments”:”4”,”Details”:”2,1,1,0,0”}
]
Name | Total | 1 | 2 | 3 | 4 | 5 |

Class 5 //displaying all class 5 data 

aaa  |  4    | 2 | 0 | 0 | 2 | 0 |  
bbb  |  1    | 1 | 0 | 0 | 0 | 0 |  

Class 6 //displaying all class 6 data

ccc  |  1    | 0 | 0 | 1 | 0 | 0 |  
ddd  |  4    | 2 | 1 | 1 | 0 | 0 |  
要按如下方式显示它:

[
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”4”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”aaa”,”class”:”5”,”commentsDay”:”4”},
    {“studName”:”bbb”,”class”:”5”,”commentsDay”:”1”},
    {“studName”:”ccc”,”class”:”6”,”commentsDay”:”3”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”1”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”3”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”2”},
    {“studName”:”ddd”,”class”:”6”,”commentsDay”:”1”} 
]
[
    {“studName”:”aaa”,”class”:”5”,”TotalComments”:”4”,”Details”:”2,0,0,2,0”},
    {“studName”:”bbb”,”class”:”5”,”TotalComments”:”1”,”Details”:”1,0,0,0,0”},
    {“studName”:”ccc”,”class”:”6”,”TotalComments”:”1”,”Details”:”0,0,1,0,0”},
    {“studName”:”ddd”,”class”:”6”,”TotalComments”:”4”,”Details”:”2,1,1,0,0”}
]
Name | Total | 1 | 2 | 3 | 4 | 5 |

Class 5 //displaying all class 5 data 

aaa  |  4    | 2 | 0 | 0 | 2 | 0 |  
bbb  |  1    | 1 | 0 | 0 | 0 | 0 |  

Class 6 //displaying all class 6 data

ccc  |  1    | 0 | 0 | 1 | 0 | 0 |  
ddd  |  4    | 2 | 1 | 1 | 0 | 0 |  
不了解如何在原始集合字符串上编写循环以在页面上显示如上所述的数据

所以我尝试以不同的格式修改原始集合字符串(如上所述,使用JavaScript),但没有成功

好心的导游

提前谢谢


Vikas

您可以迭代数组并使用所需的属性构建新对象

字符串化的结果使得维护有点复杂

var data=[{studName:'aaa',class:'5',commentsDay:'1'},{studName:'aaa',class:'5',commentsDay:'4'},{studName:'aaa',class:'5',commentsDay:'5',commentsDay:'1'},{studName:'ccc',class:'6',commentsDay:'3'},{studName:'ddd',class:'6',commentsDay:'1'},{studName:'ddd',class:'6',commentsDay:'3'},{studName:'ddd',class:'6',commentsDay:'1'},{studName:'ddd',class:'6',commentsDay:'1'},
结果=数据.reduce(函数(散列){
返回函数(r,a){
无功温度;
如果(!hash[a.studName]){
hash[a.studName]={studName:a.studName,class:a.class,TotalComments:'0',详细信息:'0,0,0,0'},
r、 push(hash[a.studName]);
}
temp=hash[a.studName].Details.split(',');
临时[a.commentsDay-1]=+temp[a.commentsDay-1]+1;
hash[a.studName].Details=temp.join();
哈希[a.studName].TotalComments=(+hash[a.studName].TotalComments+1).toString();
返回r;
}
}(Object.create(null)),[]);
console.log(结果);

.as控制台包装器{max height:100%!important;top:0;}
看起来像是的作业。我认为使用数组中的详细信息进行维护可能更容易,然后使用
join(',')进行第二次传递
谢谢你的严格化。谢谢尼娜,你给了我想要的输出。实际上我正在学习/试图理解它。他们还有其他方法吗?我的意思是,使用“过滤”功能(而不是reduce)我们能实现相同的输出或相同的表格格式吗?谢谢。实际上不是,因为过滤器返回相同的项目,但根据返回值可能会更少。好的。谢谢。我将进一步研究并实现它。再次感谢。尼娜,在我上一个示例中,我假设数据的周期在1到5之间。如果这一变化,我将如何管理它。我试图将Details数组设置为空,但不起作用。例如,Details:[]Details数组应处理1到31之间的任何日期数据。请您指导?