我有一个包含单个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
,但我在这种情况下做,因为它不太凌乱。如果有人能帮助我,我将不胜感激。
对测试部分抱歉。我忘了改变它,因为我在发布之前更改了变量,以便在提问时更有意义。感谢您的回答。我会尽快回复你。 –
你的方法更美观,并且比我上面做的更平滑。感谢您的答复。 –