javascript - How to keep rows with unique IDs once in d3.js -
i have json dataset following
var data= [ {"id":10,"marks":20}, {"id":20,"marks":30}, {"id":10,"marks":40}, {"id":10,"marks":60}, {"id":10,"marks":70} ]
i want keep rows unique ids. means should have data following rows only
[ {"id":10,"marks":20}, {"id":20,"marks":30} ]
in real thi size of file huge. , want show average mark of unique ids. making average using different code. adding different column @ reading json file. i.e.
{"id":10,"marks":20,"avg":25}
so want keep rows unique ids otherwise in visualization avg coming multiple times same id. how can in d3.js? or there option?
there many ways of getting array of unique objects. far aware, there no built-in mechanism in d3 providing functionality. have done having associative array keep track of objects present in output array. adjusted needs like:
var contains = {}, unique = []; (var = 0; < data.length; i++) { if(!contains[data[i].id]) { contains[data[i].id] = true; unique.push(data[i]); } }
see jsfiddle.
Comments
Post a Comment