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

时间:2017-02-07 12:16:53

标签: javascript linq

需要帮助来格式化我的JSON集合sting by Javascript或linq

假设在页面上显示日历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”} 
]

并按照以下格式要求: {“StudentName”,“Class”,“TotalComments”,“CommentDetails”}

所以我上面提到的集合字符串应格式如下:

[
    {“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),但没有成功。

请指导。

提前致谢。

维卡斯

1 个答案:

答案 0 :(得分:1)

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

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



var data = [{ 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' }],
    result = data.reduce(function (hash) {
        return function (r, a) {
            var temp;
            if (!hash[a.studName]) {
                hash[a.studName] = { studName: a.studName, class: a.class, TotalComments: '0', Details: '0,0,0,0,0' },
                r.push(hash[a.studName]);
            }
            temp = hash[a.studName].Details.split(',');
            temp[a.commentsDay - 1] = +temp[a.commentsDay - 1] + 1;
            hash[a.studName].Details = temp.join();
            hash[a.studName].TotalComments = (+hash[a.studName].TotalComments + 1).toString();
            return r;
        }
    }(Object.create(null)), []);
	
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }




使用字符串的收集后处理。



var data = [{ 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' }],
    detailsLength = 5;
    result = data.reduce(function (hash) {
        return function (r, a) {
            if (!hash[a.studName]) {
                hash[a.studName] = {
                    studName: a.studName,
                    class: a.class,
                    TotalComments: 0,
                    Details: Array.apply(null, { length: detailsLength }).map(function () { return 0; })
                },
                r.push(hash[a.studName]);
            }
            hash[a.studName].TotalComments++;
            hash[a.studName].Details[a.commentsDay - 1]++;
            return r;
        }
    }(Object.create(null)), []);
	
result.forEach(function (a) {
    a.TotalComments= a.TotalComments.toString();
    a.Details = a.Details.join();
});
console.log(result);

.as-console-wrapper { max-height: 100% !important; top: 0; }