【问题标题】:Javascript Reduce Array of Date Value ArraysJavascript 减少日期值数组的数组
【发布时间】:2023-03-06 00:50:02
【问题描述】:

我确信有一个简单的解决方案,但我找不到。我将 [date, value] 数组传递给 Highcharts,但我需要添加重复日期的值。

我正在寻找一个包含合并结果的新数组。

[{
  "name": "actual",
  "data": [
    [1396242000000, 592.685],
    [1396242000000, 377.712],
    [1404104400000, 316.74],
    [1404104400000, 596.709],
    [1412053200000, 579.066],
    [1412053200000, 300.803],
    [1420005600000, 596.497],
    [1420005600000, 297.004],
    [1427778000000, 435.818],
    [1427778000000, 287.556],
    [1435640400000, 446.788],
    [1435640400000, 282.971],
    [1443589200000, 270.027],
    [1443589200000, 445.239],
    [1451541600000, 258.869],
    [1451541600000, 432.285],
    [1459400400000, 266.438],
    [1459400400000, 409.761],
    [1467262800000, 408.126],
    [1467262800000, 246.83],
    [1475211600000, 395.026],
    [1475211600000, 233.635]
  ]
}, {
  "name": "forecast",
  "data": [
    [1483164000000, 406.329],
    [1483164000000, 240.611],
    [1490936400000, 414.456],
    [1490936400000, 241.814],
    [1498798800000, 243.99],
    [1498798800000, 422.745],
    [1506747600000, 443.882],
    [1506747600000, 246.43],
    [1514700000000, 466.076],
    [1514700000000, 253.823]
  ]
}]

【问题讨论】:

  • 您的预期结果是什么?是否要删除重复项?
  • 您可以使用重复数据创建另一个系列。
  • 我需要减少结果,以便为每个项目获得这样的结果... [1396242000000, 970.397] (592.685+377.72 = 970.397)

标签: javascript arrays highcharts


【解决方案1】:

var arr = [{
  "name": "actual",
  "data": [
    [1396242000000, 592.685],
    [1396242000000, 377.712],
    [1404104400000, 316.74],
    [1404104400000, 596.709],
    [1412053200000, 579.066],
    [1412053200000, 300.803],
    [1420005600000, 596.497],
    [1420005600000, 297.004],
    [1427778000000, 435.818],
    [1427778000000, 287.556],
    [1435640400000, 446.788],
    [1435640400000, 282.971],
    [1443589200000, 270.027],
    [1443589200000, 445.239],
    [1451541600000, 258.869],
    [1451541600000, 432.285],
    [1459400400000, 266.438],
    [1459400400000, 409.761],
    [1467262800000, 408.126],
    [1467262800000, 246.83],
    [1475211600000, 395.026],
    [1475211600000, 233.635]
  ]
}, {
  "name": "forecast",
  "data": [
    [1483164000000, 406.329],
    [1483164000000, 240.611],
    [1490936400000, 414.456],
    [1490936400000, 241.814],
    [1498798800000, 243.99],
    [1498798800000, 422.745],
    [1506747600000, 443.882],
    [1506747600000, 246.43],
    [1514700000000, 466.076],
    [1514700000000, 253.823]
  ]
}];

arr.forEach(function(o) {                      // for each object o in the array
  var hash = {};                               // the hash object, to store the indexes of times in the new array
  o.data = o.data.reduce(function(newArr, e) { // reduce the data array of the object o
    var index = hash[e[0]];                    // get the index from the hash object of this time (e[0])
    if(index === undefined) {                  // if index is undefined (this time is not hashed yet), then add e to the array and hash its index so it'll be used for duplicates
      hash[e[0]] = newArr.push(e) - 1;         // push returns the new length so the index is the length - 1
    } else {                                   // otherwise (the time is already hashed)
      newArr[index][1] += e[1];                // then add the value of this element (e[1]) to the value of the already hashed element at index index
    }
    return newArr;
  }, []);
});

console.log(arr);

【讨论】:

  • 这很棒。我没想到要使用哈希。非常感谢您的帮助。
【解决方案2】:

试试这个:

var actual =   [  [1396242000000, 592.685],
    [1396242000000, 377.712],
    [1404104400000, 316.74],
    [1404104400000, 596.709],
    [1412053200000, 579.066],
    [1412053200000, 300.803],
    [1420005600000, 596.497],
    [1420005600000, 297.004],
    [1427778000000, 435.818],
    [1427778000000, 287.556],
    [1435640400000, 446.788],
    [1435640400000, 282.971],
    [1443589200000, 270.027],
    [1443589200000, 445.239],
    [1451541600000, 258.869],
    [1451541600000, 432.285],
    [1459400400000, 266.438],
    [1459400400000, 409.761],
    [1467262800000, 408.126],
    [1467262800000, 246.83],
    [1475211600000, 395.026],
    [1475211600000, 233.635]];

var sum = {},result;

for (var i=0,c;c=actual[i];++i) {
    if ( undefined === sum[c[0]] ) {        
       sum[c[0]] = c;
    }
    else {
        sum[c[0]][1] += c[1];
    }
}
result = Object.keys(sum).map(function(val) { return sum[val]});

alert(JSON.stringify(result));

然后对预测数组执行相同操作。

【讨论】:

  • 感谢您的帮助。让我也试试这个。
【解决方案3】:

我会这样做(假设你能够使用 ES6):

const data = [
  [1396242000000, 592.685],
  [1396242000000, 377.712],
  [1404104400000, 316.74],
  [1404104400000, 596.709],
  [1412053200000, 579.066],
  [1412053200000, 300.803],
  [1420005600000, 596.497],
  [1420005600000, 297.004],
  [1427778000000, 435.818],
  [1427778000000, 287.556],
  [1435640400000, 446.788],
  [1435640400000, 282.971],
  [1443589200000, 270.027],
  [1443589200000, 445.239],
  [1451541600000, 258.869],
  [1451541600000, 432.285],
  [1459400400000, 266.438],
  [1459400400000, 409.761],
  [1467262800000, 408.126],
  [1467262800000, 246.83],
  [1475211600000, 395.026],
  [1475211600000, 233.635]
];

const forecast = data.reduce((acc, val) => {
  let existingForecast = acc.find(f => f[0] === val[0]);
  existingForecast ? existingForecast[1] += val[1] : acc.push(val);
  return acc;
}, []);

console.log(forecast);

【讨论】:

  • 这真是一个优雅的解决方案。我会试一试。谢谢。
  • @JohnBest 唯一需要注意的是,如果您处理大量数据,find 调用可能会变慢。我假设您使用的数据集不会比您提供的示例大得多。
猜你喜欢
  • 2021-04-30
  • 2023-02-24
  • 2021-03-10
  • 1970-01-01
  • 2020-12-07
  • 1970-01-01
  • 2014-07-16
  • 1970-01-01
  • 2021-09-27
相关资源
最近更新 更多