如何使用 javasscript 在一小时内获取数组中最新数据和先前数据之间的差异?
How to get the difference between latest and previous data in an array within one hour using javasscript?
我有这个对象数组:
0: {time: '2021-12-02T23:53:54.062Z', value: 558316}
1: {time: '2021-12-03T00:53:53.959Z', value: 558452}
2: {time: '2021-12-03T01:53:53.934Z', value: 558588}
3: {time: '2021-12-05T23:53:48.617Z', value: 568039}
4: {time: '2021-12-06T00:53:48.609Z', value: 568174}
5: {time: '2021-12-06T01:53:48.545Z', value: 568309}
6: {time: '2021-12-06T02:53:48.480Z', value: 568444}
7: {time: '2021-12-06T03:53:48.393Z', value: 568579}
问题是如何求出每小时的差值,例如1的值:558452减去0的值:558316等于136。但如果当前值和上一个值不在一小时内,则不要减去例如3的值:568039减去2的值:558588,3的值与2的值相差一个多小时。所以这两个不应该相减,因为它会在图中得到高值(9,451)。
预期结果:
{time: '2021-12-03T00:53:53.959Z', value: 136}
{time: '2021-12-03T01:53:53.934Z', value: 136}
{time: '2021-12-06T00:53:48.609Z', value: 135}
{time: '2021-12-06T01:53:48.545Z', value: 135}
{time: '2021-12-06T02:53:48.480Z', value: 135}
{time: '2021-12-06T03:53:48.393Z', value: 135}
你可以用map算出差值,然后据此过滤..
例如
var data = [
{time: '2021-12-02T23:53:54.062Z', value: 558316},
{time: '2021-12-03T00:53:53.959Z', value: 558452},
{time: '2021-12-03T01:53:53.934Z', value: 558588},
{time: '2021-12-05T23:53:48.617Z', value: 568039},
{time: '2021-12-06T00:53:48.609Z', value: 568174},
{time: '2021-12-06T01:53:48.545Z', value: 568309},
{time: '2021-12-06T02:53:48.480Z', value: 568444},
{time: '2021-12-06T03:53:48.393Z', value: 568579},
].map(d => (d.time = new Date(d.time), d));
const oneHour = 1000 * 60 * 60;
const temp = data.map((m, ix, a) => {
if (ix > 0) {
m.diff = a[ix].time - a[ix -1].time;
m.value2 = a[ix].value - a[ix -1].value;
} else {
//make first one filter out as
//there is no previous
m.diff = oneHour + 1;
}
return m;
})
.filter(m => m.diff < oneHour)
.map(m => ({
time: m.time,
value: m.value2
}));
console.log(temp);
我有这个对象数组:
0: {time: '2021-12-02T23:53:54.062Z', value: 558316}
1: {time: '2021-12-03T00:53:53.959Z', value: 558452}
2: {time: '2021-12-03T01:53:53.934Z', value: 558588}
3: {time: '2021-12-05T23:53:48.617Z', value: 568039}
4: {time: '2021-12-06T00:53:48.609Z', value: 568174}
5: {time: '2021-12-06T01:53:48.545Z', value: 568309}
6: {time: '2021-12-06T02:53:48.480Z', value: 568444}
7: {time: '2021-12-06T03:53:48.393Z', value: 568579}
问题是如何求出每小时的差值,例如1的值:558452减去0的值:558316等于136。但如果当前值和上一个值不在一小时内,则不要减去例如3的值:568039减去2的值:558588,3的值与2的值相差一个多小时。所以这两个不应该相减,因为它会在图中得到高值(9,451)。
预期结果:
{time: '2021-12-03T00:53:53.959Z', value: 136}
{time: '2021-12-03T01:53:53.934Z', value: 136}
{time: '2021-12-06T00:53:48.609Z', value: 135}
{time: '2021-12-06T01:53:48.545Z', value: 135}
{time: '2021-12-06T02:53:48.480Z', value: 135}
{time: '2021-12-06T03:53:48.393Z', value: 135}
你可以用map算出差值,然后据此过滤..
例如
var data = [
{time: '2021-12-02T23:53:54.062Z', value: 558316},
{time: '2021-12-03T00:53:53.959Z', value: 558452},
{time: '2021-12-03T01:53:53.934Z', value: 558588},
{time: '2021-12-05T23:53:48.617Z', value: 568039},
{time: '2021-12-06T00:53:48.609Z', value: 568174},
{time: '2021-12-06T01:53:48.545Z', value: 568309},
{time: '2021-12-06T02:53:48.480Z', value: 568444},
{time: '2021-12-06T03:53:48.393Z', value: 568579},
].map(d => (d.time = new Date(d.time), d));
const oneHour = 1000 * 60 * 60;
const temp = data.map((m, ix, a) => {
if (ix > 0) {
m.diff = a[ix].time - a[ix -1].time;
m.value2 = a[ix].value - a[ix -1].value;
} else {
//make first one filter out as
//there is no previous
m.diff = oneHour + 1;
}
return m;
})
.filter(m => m.diff < oneHour)
.map(m => ({
time: m.time,
value: m.value2
}));
console.log(temp);