将数组分组并计算基于组创建新数组的项目

时间:2016-11-30 14:53:44

标签: javascript

我对操作数组的知识真的很差,如果我可以得到任何帮助,我就会徘徊。我有一个array("dataResult")。它有区域,水果和用户信息。我想基于Region对此信息进行分组,并计算每个组的唯一用户数。

dataResult = [{ region: "Africa", fruit: "Orange", user: "Gary" }, 
              { region: "Africa", fruit: "Apple", user: "Steve" },
              { region: "Europe", fruit: "Orange", user: "John" }, 
              { region: "Europe", fruit: "Apple", user: "bob" }, 
              { region: "Asia", fruit: "Orange", user: "Ian" }, 
              { region: "Asia", fruit: "Apple", user: "Angelo" }, 
              { region: "Africa", fruit: "Orange", user: "Gary" }]`

我希望我的最终数组看起来像下面的数组,以反映每个组的分组结果和唯一身份用户数

NewResult = [{ region: "Africa", count: 2 }, { region: "Europe", count: 2},{ region: "Asia", count: 2}],

关于如何实现这一点的任何想法?我已经看到了关于如何对结果进行分组的一些很好的信息,但是我很难为每个组获得唯一身份用户的计数。

分组代码

var categoryNames = groupBy(dataResults, '2');
console.log(categoryNames);

function groupBy(items,propertyName)
{
    var result = [];
    $.each(items, function(index, item) {
      if ($.inArray(item[propertyName], result)==-1) {
          result.push(item[propertyName]);
      }
    });
    return result;
}

2 个答案:

答案 0 :(得分:0)

使用适当的数据,您可以迭代对象数组并计算区域。

var dataResult = [{ region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" }],
    grouped = [];

dataResult.forEach(function (a) {
    if (!this[a.region]) {
        this[a.region] = { region: a.region, count: 0 };
        grouped.push(this[a.region]);
    }
    this[a.region].count++;
}, Object.create(null));
  
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

对于唯一身份用户,您可以使用扩展哈希表

var dataResult = [{ region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" }],
    grouped = [];

dataResult.forEach(function (a) {
    var key = [a.region, a.user].join('|');
    if (!this[a.region]) {
        this[a.region] = { region: a.region, count: 0 };
        grouped.push(this[a.region]);
    }
    if (!this[key]) {
        this[key] = true;
        this[a.region].count++;
    }
}, Object.create(null));
  
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

部分地看一下哈希表和代码中的一些解释。

var dataResult = [{ region: "Africa", fruit: "Orange", user: "Gary" }, { region: "Africa", fruit: "Apple", user: "Steve" }, { region: "Europe", fruit: "Orange", user: "John" }, { region: "Europe", fruit: "Apple", user: "bob" }, { region: "Asia", fruit: "Orange", user: "Ian" }, { region: "Asia", fruit: "Apple", user: "Angelo" }, { region: "Africa", fruit: "Orange", user: "Gary" }],
    grouped = [],               // result array
    hash = Object.create(null); // oposite of {}, this object does not contain any prototypes

dataResult.forEach(function (a) {

    // build key with region + name, spot pipe in hash
    var key = [a.region, a.user].join('|');

    // check if region is not in hash table
    if (!this[a.region]) {

        // create new object with region and count and insert it into hash table
        this[a.region] = { region: a.region, count: 0 };

        // push the object to the result
        grouped.push(this[a.region]);
    }

    // check if if not region and user exists, it means
    // the user in the region is not counted yet.
    if (!this[key]) {

        //create hash entry
        this[key] = true;

        // increment count 
        this[a.region].count++;
    }
}, hash);
  
console.log(hash);
console.log(grouped);
.as-console-wrapper { max-height: 100% !important; top: 0; }

答案 1 :(得分:0)

您要做的是将数组转换为对象,因为最终数组不是有效数组。

所以你想要返回看起来像这样的东西。

import matplotlib.pyplot as plt

def trajectory(listOfObjects, time):
   #Stuff happens here
   plt.scatter(x,y)

您需要修改一个迭代数组的函数,并返回一个具有所需键和值对的新对象。看看地图并减少Javascript中的功能,它们可能会有所帮助。

降低 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/Reduce

地图 https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/map