按java脚本分组Array对象

时间:2016-05-28 12:48:47

标签: javascript arrays algorithm

我在JavaScript中有以下数组对象

  [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ]

根据语言及其在java脚本中的平均值对数组项进行分组的最佳方法是什么。

我使用下面的代码进行分组。

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

5 个答案:

答案 0 :(得分:2)

&#13;
&#13;
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);
&#13;
&#13;
&#13;

答案 1 :(得分:0)

概括方法:

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

答案 2 :(得分:0)

与输入数组具有相同风格的提案。

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

答案 3 :(得分:0)

试试这段代码..

&#13;
&#13;
var items = [ ["English", 52], ["Hindi", 154], ["Hindi", 241], ["Spanish", 10], ["French", 65], ["German", 98], ["Russian", 10] ];
for(var i=0;i<items.length;i++)
{
  var sum=items[i][1],cnt=1,avg=0;
  for(var j=i+1;j<items.length;j++)
  {    
    if(items[i][0] == items[j][0]){
      sum+=items[j][1];
      cnt++;
      items.splice(j, 1);
    } 
  }
  avg = sum/cnt;
  items[i][1] = avg;
}

console.log(items);
&#13;
&#13;
&#13;

答案 4 :(得分:0)

好吧..对于一个改变,让我们用一个简化来做这个O(n)。

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