簡體   English   中英

如何從 3 個二維數組中獲取累積總數組

[英]how to get a cumulative total array from 3 two dimensional arrays

我有 3 個二維數組,如下所示,它們是用於在圖形上繪制線條的系列數據,鍵是時間戳。

const arr1 = [[1641013200000,1881],[1643691600000,38993],[1646110800000,41337],[1648785600000,78856],[1651377600000,117738],[1654056000000,119869],[1656648000000,157799],[1659326400000,196752],[1662004800000,199061],[1664596800000,237034],[1667275200000,239153],[1669870800000,269967]]
const arr2 = [[1641013200000,1302],[1643691600000,3347],[1646110800000,4754],[1648785600000,6948],[1651377600000,9725],[1654056000000,11314],[1656648000000,13787],[1659326400000,16666],[1662004800000,18370],[1664596800000,20876],[1667275200000,22384],[1669870800000,23560]]
const arr3 = [[1643691600000,67350],[1648785600000,134700],[1651377600000,202148],[1654056000000,202270],[1656648000000,269843],[1659326400000,337346],[1662004800000,337470],[1664596800000,404861],[1667275200000,404889],[1669870800000,472239]]

我想繪制另一條系列線,給出所有三個數組值的累計總和(注意:如果兩個數組中都不存在時間戳,請添加先前的索引值)

const totalArray = [
[1641013200000,3183],[1643691600000, 109690],[1646110800000, 113441],[1648785600000, 220504],
[1651377600000, 329611],[1654056000000, 333453],[1656648000000, 441429],[1659326400000, 550764],
[1662004800000, 554901],[1664596800000, 662771],[1667275200000, 666426],[1669870800000, 765766]
]

我試過這個,但是由於時間戳不存在於任何一個中,所以有些值不正確

方法:

const arr1 = [[1641013200000,1881],[1643691600000,38993],[1646110800000,41337],[1648785600000,78856],[1651377600000,117738],[1654056000000,119869],[1656648000000,157799],[1659326400000,196752],[1662004800000,199061],[1664596800000,237034],[1667275200000,239153],[1669870800000,269967]];
const arr2 = [[1641013200000,1302],[1643691600000,3347],[1646110800000,4754],[1648785600000,6948],[1651377600000,9725],[1654056000000,11314],[1656648000000,13787],[1659326400000,16666],[1662004800000,18370],[1664596800000,20876],[1667275200000,22384],[1669870800000,23560]];
const arr3 = [[1643691600000,67350],[1648785600000,134700],[1651377600000,202148],[1654056000000,202270],[1656648000000,269843],[1659326400000,337346],[1662004800000,337470],[1664596800000,404861],[1667275200000,404889],[1669870800000,472239]];

const calculateTotal = () => {
    var ret;

    for(let a3 of arr3) {
      var index = arr1.map(function(el){return el[0];}).indexOf(a3[0]);
      console.log(index);
      if (index === -1) {
        ret = arr1[index][0];
        console.log(ret);
      }
    }
    let unsortedArr = arr1.concat(arr2, arr3);
    var sortedArray = unsortedArr.sort((a, b) => a[0] - b[0]);
    var added = addArray(sortedArray);
    console.log("Curent Output: " + JSON.stringify(added));
} 

const addArray = (tuples) => {
    var hash = {},
      keys = [];
      tuples.forEach(function (tuple) {
        var key = tuple[0],
            value = tuple[1];
        if (hash[key] === undefined) {
          keys.push(key);
          hash[key] = value;
        } else {
          hash[key] += value;
        }
      });
      return keys.map(function (key) {
        return([key, hash[key]]);
      });
}

calculateTotal();

[代碼沙盒鏈接][1] [1]:https://codesandbox.io/s/tender-blackburn-rgwns3?file=/src/App.js:1349-1761

有可能實現這一目標嗎?

在你的代碼中有這樣的:

if (index === -1) {
    ret = arr1[index][0];

但由於arr1[-1]未定義,該分配將失敗。

然后當你這樣做時:

let unsortedArr = arr1.concat(arr2, arr3);

...當三個數組中的任何一個具有“缺失”時間戳時,您最終得到一個不知道使用默認值(來自先前索引)的數組。

我會建議這種方法:

  1. 將所有唯一時間戳(來自所有數組)收集到一個 Map 中,並將數組與這些鍵中的每一個相關聯:這些鍵最初是空的。
  2. 使用原始數組中的時間戳填充這些數組
  3. 從該地圖中獲取排序后的條目列表
  4. 當相應的插槽未定義時,通過將值傳遞到下一個數組來填補“空白”。 同時將這些值相加為最終輸出。

這是一個實現:

 function mergeArrays(...arrays) { const map = new Map(arrays.flatMap(arr => arr.map(([stamp]) => [stamp, []]))); arrays.forEach((arr, i) => { for (const [timeStamp, value] of arr) { map.get(timeStamp)[i] = value; } }); const state = Array(arrays.length).fill(0); return Array.from(map).sort(([a], [b]) => a - b).map(([timeStamp, arr], i) => [timeStamp, state.reduce((sum, prev, j) => sum + (state[j] = arr[j]?? prev), 0)] ); } // Example run const arr1 = [[1641013200000,1881],[1643691600000,38993],[1646110800000,41337],[1648785600000,78856],[1651377600000,117738],[1654056000000,119869],[1656648000000,157799],[1659326400000,196752],[1662004800000,199061],[1664596800000,237034],[1667275200000,239153],[1669870800000,269967]]; const arr2 = [[1641013200000,1302],[1643691600000,3347],[1646110800000,4754],[1648785600000,6948],[1651377600000,9725],[1654056000000,11314],[1656648000000,13787],[1659326400000,16666],[1662004800000,18370],[1664596800000,20876],[1667275200000,22384],[1669870800000,23560]]; const arr3 = [[1643691600000,67350],[1648785600000,134700],[1651377600000,202148],[1654056000000,202270],[1656648000000,269843],[1659326400000,337346],[1662004800000,337470],[1664596800000,404861],[1667275200000,404889],[1669870800000,472239]]; const result = mergeArrays(arr1, arr2, arr3); console.log(result);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM