【问题标题】:Grouping an Array and Counting items creating new array based on Groups对数组进行分组并计算基于组创建新数组的项目
【发布时间】:2016-12-01 17:37:39
【问题描述】:

我在操作数组方面的知识真的很差,如果我能得到任何帮助,我正在徘徊。我有一个array("dataResult") 。它有地区、水果和用户信息。我想根据地区对这些信息进行分组,并计算每个组的唯一用户数。

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

【问题讨论】:

标签: javascript


【解决方案1】:

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

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

【讨论】:

  • 非常感谢代码 sn-p Nina,我很抱歉我懒惰地尝试正确输入数组。我注意到您的结果显示非洲的计数为 3,有没有办法避免重复名称。由于 Gary 出现 2,我希望它算作 1。我如何修改您已经需要处理的重复名称
  • 非常感谢尼娜。这非常适合我的情况。仅用于学习目的和不熟悉此的人。你能解释一下代码是如何工作的吗?也许是一些cmets。不过真的很感谢
  • @OgbobeBuko,请查看最后的 sn-p。如果您有任何问题,请不要犹豫:)
【解决方案2】:

你想要做的是将你的数组变成一个对象,因为你的最终数组不是一个有效的数组。

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

 newResults = {Africa: 2, Europe: 2, Asia: 2}

您需要对遍历数组并返回具有所需键值对的新对象的函数进行修正。看看 Javascript 中的 map 和 reduce 函数,它们可能会有所帮助。

减少 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

【讨论】:

  • 我现在意识到我的错误是无效的数组,所以我现在已经纠正了它,以帮助像我这样的新手,也感谢链接。是的,我正在尝试创建一个新对象来保存一个区域内唯一用户的数量。
  • 别担心,我们都去过那里
猜你喜欢
  • 1970-01-01
  • 2018-11-27
  • 2018-01-12
  • 2015-07-21
  • 2011-11-27
  • 2020-04-25
  • 1970-01-01
  • 1970-01-01
  • 2020-10-24
相关资源
最近更新 更多