给出图表的值列表,如下面的arrays
var arrays = [["A", [
[1391032800000, 20],
[1389826800000, 4],
[1389913200000, 4],
[1390086000000, 6]
]
]];
var dates = arrays[0][1].sort(function(x, y) { return x[0] - y[0]; });
var map = dates.map(function(dt) { return [new Date(dt[0]), dt[1]]; });
console.log(map);
地图包含:
我需要在{1}}变量中为1月16日到1月30日结束日期间缺失的所有日期添加额外的Array(2)
值。
什么是填补数组中缺失的零值(每个缺失的一天)的最快方法。
答案 0 :(得分:1)
您可以使用import os
os.system('C:\Windows\System32\calc.exe')
方法,然后使用getDate
方法创建新数组,并在每两个日期之间获得差异,并用零填充该差异。
reduce
答案 1 :(得分:0)
假设当天的时间相同并且基于Darren Sweeney的建议:
const data = [
[1391032800000, 20],
[1389826800000, 4]
];
// getting min and max dates in the array
const timestamps = data.map(([timestamp]) => timestamp);
const [min, max] = [Math.min(...timestamps), Math.max(...timestamps)];
// creating hash where evrey day in the range between min and max dates has a value of 0
let dataObj = {};
let tempTimestamp = min;
while (tempTimestamp <= max) {
dataObj[tempTimestamp] = 0;
tempTimestamp += 86400000;
}
// lopping through the original array and populating a "zero-valued" hash with values from original array
data.forEach(([timestamp, value]) => {
dataObj[timestamp] = value;
})
// converting a hash into array
const result = Object.keys(dataObj).map(timestamp => ([timestamp, dataObj[timestamp]]));