JavaScript Multidimensional Array - Object Linking -
i working javascript multidimensional array.
here scenario:-
i have educational institution user can take classes subject. subjects not fixed vary. there exam day suppose there php lang faculty enter subject name student name , marks. if student enrolled himself more 1 subject marks listed in same row.
for example mr. anand has enrolled php , html , mr. deep has enrolled himself php only.
additionally want show minimum , maximum marks well.
so on result day result card
name\subject | php | html | java -------------------------------------- anand | 80 | 60 | -- deep | 70 | -- | -- sachin | 55 | 56 | 45 on ... | -- | -- | 80 -------------------------------------- min marks | 70 | 56 | 45 max mark | 80 | 60 | 80
i have created multidimensional array unable reproduce code per visual. think doing wrong.
below code have created of :-
var data = [ ["html", [{ "name": "anand", "marks": 90 }, { "name": "deep", "marks": 79 }, { "name": "raman", "marks": 34 }]], ["php", [{ "name": "anand", "marks": 90 }, { "name": "ekam", "marks": 40 }]] ] (var = 0; < data.length; i++) { document.write("<h2> " + data[i][0] + " </h2>"); var secondleveldata = data[i][1], secondlen = secondleveldata.length; (var j = 0; j < secondlen; j++) { document.write(secondleveldata[j].name + " -- " + secondleveldata[j].marks + " <br/>"); } }
please me desired result. working on it.
thanks help!!
by changing json can achieve this. added jquery , underscore libraries dom , array manipulations
js fiddle link : https://jsfiddle.net/8lb7x01u/3/
var data = [ { "name": "anand", "score": [ { "subject": "html", "marks": 90 }, { "subject": "php", "marks": 90 } ] }, { "name": "deep", "score": [ { "subject": "html", "marks": 79 } ] }, { "name": "raman", "score": [ { "subject": "html", "marks": 34 } ] }, { "name": "ekam", "score": [ { "subject": "php", "marks": 40 } ] } ]; var allscores = _.pluck(data,"score"); var allsubjects = _.groupby(_.flatten(allscores),"subject"); var allstudents = _.pluck(data,"name"); var headerrow = $("<tr></tr>"); $("<th></th>").html("name\\subject").appendto(headerrow); for(var subject in allsubjects){ $("<th></th>").html(subject).appendto(headerrow); } headerrow.appendto(".scorecard"); for(var i=0;i<allscores.length;i++){ var individualscores = _.groupby(allscores[i],"subject"); var tr = $("<tr></tr>"); $("<td></td>").html(allstudents[i]).appendto(tr); for(var subject in allsubjects) { if(individualscores[subject]){ $("<td></td>").html(individualscores[subject][0].marks).appendto(tr); }else { $("<td></td>").html("...").appendto(tr); } } tr.appendto(".scorecard tbody") } rendermaxmin("max"); rendermaxmin("min"); function rendermaxmin(param){ var footerrow = $("<tr></tr>"); $("<td></td>").html(param+" marks").appendto(footerrow); for(var subject in allsubjects){ var marks = _.pluck(allsubjects[subject],"marks"); var value =(param === "max") ? _.max(marks) : _.min(marks); $("<td></td>").html(value).appendto(footerrow); } footerrow.appendto(".scorecard tfoot") }
Comments
Post a Comment