javascript - 按第一个元素对嵌套数组进行分组

标签 javascript arrays

我有一个包含单独的 2 元素数组的数组,每个数组在第一个单元格中包含一个以毫秒为单位的日期,在第二个单元格中包含一个数字(交易编号)。它看起来像这样:

var array = [[12135435123, -2],
[12135435123, 1], 
[12135464565, -2], 
[12423675834, 0], 
[12423675834, 1]....];

并非每个单元格中所有以毫秒为单位的日期都相同,上面的日期完全是我编造的,但逻辑是相同的。

我想要做的是创建另一个与上面结构相同的数组(全局数组内的2元素数组)。全局数组中的每个元素都是一个 2 元素数组,表示一组具有相同日期的数组,如下所示:[transactionsCount, netTransaction]

transactionsCount 是唯一日期实例的计数,或唯一日期内的交易数量。 netTransaction 是该组中第二个单元格的总和,或某个日期的净交易值。

使用上面的示例,我希望最终的数组如下所示:

var newArray = [[2, -1], [1,-2],[2,1]...];
// The first array is 2 and -1 because there are 2 transactions for the unique day and the -1 is the net transaction amount. The 2nd array is 1,-2 because there is only 1 transaction for the day with a net total of -2, etc, etc.

我所做的是创建一个为我执行此操作的原始函数,但我遇到了问题,因为在某些情况下该函数会过度计算唯一天数并给出错误的结果。所以我在这里做错了一些事情。这是我所拥有的:

transactions = function transactions(array){
  var transactionCount = 0;
  var netTransactionCounter = 0;
  var finishedArray = [];
  var tempDateArray = [];

  array.forEach(function(item){
    var init = [];
    if(tempDateArray.length == 0){
      tempDateArray.push(item[0]);
      transactionCount++;
      netTransactionCounter += Number(item[1]);
    } else if(tempDateArray.length > 0){
      if(item[0] == tempDateArray[0]){
        transactionCount++;
        netTransactionCounter += Number(item[1]);
      } else if(item[0] !== tempDateArray[0]){
        tempDateArray.pop();
        tempDateArray.push(item[0]);
        init.push(transactionCount);
        init.push(netTransactionCounter);
        finishedArray.push(init);
        transactionCount = 1;
        netTransactionCounter = Number(item[1]);
      }
    }
  });

  return finishedArray;

}

我真的不喜欢使用forEach,但我在这种情况下使用了,因为它不那么困惑。如果有人能帮助我解决这个问题,我将不胜感激。

最佳答案

我很难弄清楚你的逻辑,尤其是对于那个未定义的testing数组。

我将使用字典逻辑来收集日期作为唯一键。另外,它节省了管理长度计算的精力(使用您定义的所有计数器)。

我想说下面的代码是非常 self 解释的,但是如果您在理解它时遇到问题,请不要急于询问。

var array = [[12135435123, -2],
[12135435123, 1], 
[12135464565, -2], 
[12423675834, 0], 
[12423675834, 1]];

function transform (array) {
 var dictionary = {};
 for (var i = 0 ; i < array.length ; i++){
   var date = array[i][0].toString();
   var value = array[i][1];

   // add the date to the dictionary if it's not already there
   if ((date in dictionary) === false)
     dictionary[date] = [0, 0];
   // update the count
   dictionary[date][0]++;
   // update the net sum
   dictionary[date][1] += value;      
 }

 // transform dictionary values to array
 var result = [];
 for (var key in dictionary) {
    if (Object.prototype.hasOwnProperty.call(dictionary, key)) {
        var val = dictionary[key];
        result.push(val);
    }
 }
 return result;
}

alert(transform(array));

这是一个 JSFiddle

关于javascript - 按第一个元素对嵌套数组进行分组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34928150/

相关文章:

javascript - 使用 firebase .on() 刷新页面后,仅从 firebase 获取最后的数据

java - 对未排序数组中的正整数进行排序

javascript - 如何诊断 Controller 中的错误?

javascript - 知道是否添加了一个类(使用 javascript 添加)

c - 定义一个非常大的double类型的二维数组

python - PyPy 和高效数组

java - 生命游戏 - 越界数组索引

php - 遍历数组(+100 个值)

javascript - 调用 NPObject 上的方法时出错!在 JavaScript 中

javascript - 为什么在使用 JQuery().html() 时 .innerHTML 问题不是问题?