【发布时间】:2021-04-08 21:37:27
【问题描述】:
我正在使用如下所示的多维数组
[
[203, 541, 5001, 6.8, 207, 30252],
[203, 542, 5001, 16.3, 83, 50832],
[203, 542, 5001, 60.9, 207, 30252],
[203, 542, 5003, null, 207, 30252],
[203, 541, 5001, 15, 965, 52452],
[203, 542, 5003, 6.8,207, 30252],
[203, 542, 5003, null, 207, 30252],
[203, 541, 5001, 15, 965, 52452],
[203, 542, 5003, 6.8,207, 30252]
]
前 3 个元素是 ID 整数,后 3 个元素是浮点数。我试图能够根据前 3 个元素对这些元素进行分组并(单独)添加计数,因此我将以如下所示的数组结尾
[
[203, 541, 5001, 36.8, 1937, 30252], // Sum the last 3 elements that have 203,541,5001 as the beginning 3 elements of the array
[203, 542, 5001, 77.2, 290, 81084], // Sum the last 3 elements that have 203,541, 5001 as the beginning 3 elements of the array
[203, 541, 5003, a, b, c], // same as above
[203, 542, 5003, x, y, z] // same as above
]
如果不循环遍历所有数组,创建临时计数器,然后循环遍历每个数组元素以将最后 3 个元素添加到临时计数器,然后将结果推送到结果数组,有没有办法将这些分组并添加到同一时间?
【问题讨论】:
-
到目前为止你得到了什么代码?
-
基本上循环遍历数组,创建临时计数器(作为前 3 个元素),循环遍历每个内部数组,并添加到临时计数器,想知道是否有更好的方法来做到这一点
标签: javascript arrays