user3172224 user3172224 - 1 year ago 58
Javascript Question

Format JSON collection string by JavaScript or Linq

Need help to format my JSON collection sting by Javascript or linq

Suppose on page, displaying data for calendar day 1 to 5.

My JSON string looks like below:

[
{“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”}
]


And want it as per format shown below:
{“StudentName”, “Class”, “TotalComments” ,”CommentDetails”}

So my above mention collection string should format like below:

[
{“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”}
]


Want to display it like below:

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 |


Do not understand how to write a loop on original collection string to display data on page like above.

So I tried to modify original collection string in different format (as mention above by using JavaScript), but not succeeded.

Kindly guide.

Thanks in advance.

Vikas

Answer Source

You could iterate the array and build new objects with the wanted properties.

The stringified results makes a bit complicated to maintain.

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; }

With post collecting process for strings.

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; }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download