Group by java-script Array object
Group by java-script Array object
I have below array object in JavaScript
[ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ]
What will be the best way to group by array item based on language and their average values in java-script.
I am using below code to do grouping.
function (Scores) { var map = {}; for (var i = 0; i < Scores.length; i++) { var score = map[Scores[i][0]]; if (score) { score = { 'Sum': score.Sum + Scores[i][1], 'Count': score.Match + 1, 'Language': Scores[i][0] }; score.Avg = Math.round(score.Sum / score.Count); map[Scores[i][0]] = score; } else { map[Scores[i][0]] = { 'Sum': Scores[i][1], 'Count': 1, 'Language': Scores[i][0], 'Avg': Scores[i][1] }; } } return map; }
Answer by gu mingfeng for Group by java-script Array object
var data = [["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10]]; var aggregate = data.reduce(function(prev,curr){ var key = curr[0]; if(!prev[key]){ prev[key]={lang:key,count:0,total:0}; } var dt = prev[key]; dt.count++; dt.total+=curr[1]; dt.avg=dt.total/dt.count; return prev; },{}); console.log(aggregate);
Answer by Dhaval for Group by java-script Array object
The generalize method:
var groupList = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ] function groupByProperty (groupList, groupIndex) { var groupBy = {}; groupList.forEach(function(group){ if(groupBy[group[groupIndex]]){ groupBy[group[groupIndex]].push(group); } else { groupBy[group[groupIndex]] = []; groupBy[group[groupIndex]].push(group) } }) for(key in groupBy){ if (groupBy.hasOwnProperty(key)) { console.log(key + ',' + JSON.stringify(groupBy[key])) } } } groupByProperty(groupList,0)//For state groupByProperty(groupList,1)//For average
Answer by Nina Scholz for Group by java-script Array object
A proposal with the same style as the input array.
var data = [["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10]], result = []; data.forEach(function (a) { if (!this[a[0]]) { this[a[0]] = { data: [], result: [a[0], 0] }; result.push(this[a[0]].result); } this[a[0]].data.push(a[1]); this[a[0]].result[1] = this[a[0]].data.reduce(function (a, b) { return a + b; }) / this[a[0]].data.length; }, Object.create(null)); console.log(result);
Answer by Mohammed Shafeek for Group by java-script Array object
Try this code..
var items = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ]; for(var i=0;i
Answer by Redu for Group by java-script Array object
Well OK.. for a change lets do this O(n) with a single reduce.
var data = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ], avrg = (a) => a.reduce((p,c) => p+c)/a.length, reduced = data.reduce((p,c,i,a) => ((a[c[0]]) ? (a[c[0]].v.push(c[1]), p[a[c[0]].j][1] = avrg(a[c[0]].v)) : (a[c[0]] = {"v":[c[1]], "j":p.length}, p.push([c[0],c[1]])), p),[]); console.log(reduced);
Fatal error: Call to a member function getElementsByTagName() on a non-object in D:\XAMPP INSTALLASTION\xampp\htdocs\endunpratama9i\www-stackoverflow-info-proses.php on line 72
0 comments:
Post a Comment