从 'current time' 计算具有最近几天、几周和几个月的时间戳的对象(在数组中)
Count objects (in an array) having timestamps of last few days, weeks and months from 'current time'
我有一个对象数组,时间戳为 属性:
示例输入:
const data = [
{
_id: "602102db3acc4515d4b2f687",
createdDt: "2021-02-08T09:22:35.000Z",
},
{
_id: "6021024da706a260d8932da2",
createdDt: "2021-02-08T09:20:13.000Z",
},
// ...
// ...
{
_id: "6020fd863acc4515d4b2f685",
createdDt: "2021-02-08T08:59:50.000Z",
},
];
现在从当前日期开始,我需要 counts 个时间戳为今天[=的对象总数42=]、前一天等(如果前一天没有进入,计数可以为零)
同理,我需要计数这一天,前一天等等,周和月也是如此。
例如,我期望的输出可能是这样的:
示例输出:
const result = {
days: [0, 0, 5, 10, ...],
weeks: [15, 5, 8, 0, ...],
months: [30, 42, 33, 23, ...]
}
我正在使用 ES6、Lodash 和 moment.js。这是用于基本图形表示。
更新:
这是我写的代码,有人可以提出更简单的解决方案吗?
我目前的解决方案:
for (var i = 0, k = 1; i < 365; i++, k++) {
let dt = moment().subtract(i, "days");
let td = moment().subtract(k, "days");
builddays.push(0);
for (var j = 0; j < drivers.length; j++) {
let ddt = new Date(drivers[j].createdDt);
if (moment(ddt).isBetween(td, dt)) builddays[i] = drivers[j].count;
}
}
var weeksbifurcate = builddays.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / 7);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [];
}
resultArray[chunkIndex].push(item);
return resultArray;
}, []);
var monthsbifurcate = builddays.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / 30);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [];
}
resultArray[chunkIndex].push(item);
return resultArray;
}, []);
for (i = 0; i < 12; i++) {
days.push(builddays[i]);
weeks.push(weeksbifurcate[i].reduce(getSum, 0));
months.push(monthsbifurcate[i].reduce(getSum, 0));
}
使用moment # diff查找索引,即dayIndex
、weekIndex
、monthIndex
并在相应的[=16=中放入或增加右边的index
] 在 result
对象中:
const solve = (data) => {
const result = {
days: [],
weeks: [],
months: [],
};
data.forEach((item) => {
const today = moment();
const createdDt = moment(item.createdDt);
const dayIndex = today.diff(createdDt, "days");
const weekIndex = today.diff(createdDt, "weeks");
const monthIndex = today.diff(createdDt, "months");
result.days[dayIndex] = (result.days[dayIndex] || 0) + 1;
result.weeks[weekIndex] = (result.weeks[weekIndex] || 0) + 1;
result.months[monthIndex] = (result.months[monthIndex] || 0) + 1;
});
return result;
};
const data = [
{createdDt: "2021-03-03T00:00:00.000Z",},
{createdDt: "2021-03-03T00:00:00.000Z",},
{createdDt: "2021-03-01T00:00:00.000Z",},
{createdDt: "2021-02-28T00:00:00.000Z",},
{createdDt: "2021-02-27T00:00:00.000Z",},
{createdDt: "2021-02-27T00:00:00.000Z",},
{createdDt: "2021-02-27T00:00:00.000Z",},
{createdDt: "2021-02-24T00:00:00.000Z",},
{createdDt: "2021-02-23T00:00:00.000Z",},
];
console.log(solve(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
结果中的undefined
值可能很少,但是没有必要设置为0
,因为无论你去哪里都可以认为是0
show/use它。
我有一个对象数组,时间戳为 属性:
示例输入:
const data = [
{
_id: "602102db3acc4515d4b2f687",
createdDt: "2021-02-08T09:22:35.000Z",
},
{
_id: "6021024da706a260d8932da2",
createdDt: "2021-02-08T09:20:13.000Z",
},
// ...
// ...
{
_id: "6020fd863acc4515d4b2f685",
createdDt: "2021-02-08T08:59:50.000Z",
},
];
现在从当前日期开始,我需要 counts 个时间戳为今天[=的对象总数42=]、前一天等(如果前一天没有进入,计数可以为零)
同理,我需要计数这一天,前一天等等,周和月也是如此。
例如,我期望的输出可能是这样的:
示例输出:
const result = {
days: [0, 0, 5, 10, ...],
weeks: [15, 5, 8, 0, ...],
months: [30, 42, 33, 23, ...]
}
我正在使用 ES6、Lodash 和 moment.js。这是用于基本图形表示。
更新:
这是我写的代码,有人可以提出更简单的解决方案吗?
我目前的解决方案:
for (var i = 0, k = 1; i < 365; i++, k++) {
let dt = moment().subtract(i, "days");
let td = moment().subtract(k, "days");
builddays.push(0);
for (var j = 0; j < drivers.length; j++) {
let ddt = new Date(drivers[j].createdDt);
if (moment(ddt).isBetween(td, dt)) builddays[i] = drivers[j].count;
}
}
var weeksbifurcate = builddays.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / 7);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [];
}
resultArray[chunkIndex].push(item);
return resultArray;
}, []);
var monthsbifurcate = builddays.reduce((resultArray, item, index) => {
const chunkIndex = Math.floor(index / 30);
if (!resultArray[chunkIndex]) {
resultArray[chunkIndex] = [];
}
resultArray[chunkIndex].push(item);
return resultArray;
}, []);
for (i = 0; i < 12; i++) {
days.push(builddays[i]);
weeks.push(weeksbifurcate[i].reduce(getSum, 0));
months.push(monthsbifurcate[i].reduce(getSum, 0));
}
使用moment # diff查找索引,即dayIndex
、weekIndex
、monthIndex
并在相应的[=16=中放入或增加右边的index
] 在 result
对象中:
const solve = (data) => {
const result = {
days: [],
weeks: [],
months: [],
};
data.forEach((item) => {
const today = moment();
const createdDt = moment(item.createdDt);
const dayIndex = today.diff(createdDt, "days");
const weekIndex = today.diff(createdDt, "weeks");
const monthIndex = today.diff(createdDt, "months");
result.days[dayIndex] = (result.days[dayIndex] || 0) + 1;
result.weeks[weekIndex] = (result.weeks[weekIndex] || 0) + 1;
result.months[monthIndex] = (result.months[monthIndex] || 0) + 1;
});
return result;
};
const data = [
{createdDt: "2021-03-03T00:00:00.000Z",},
{createdDt: "2021-03-03T00:00:00.000Z",},
{createdDt: "2021-03-01T00:00:00.000Z",},
{createdDt: "2021-02-28T00:00:00.000Z",},
{createdDt: "2021-02-27T00:00:00.000Z",},
{createdDt: "2021-02-27T00:00:00.000Z",},
{createdDt: "2021-02-27T00:00:00.000Z",},
{createdDt: "2021-02-24T00:00:00.000Z",},
{createdDt: "2021-02-23T00:00:00.000Z",},
];
console.log(solve(data));
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.29.1/moment.min.js"></script>
结果中的undefined
值可能很少,但是没有必要设置为0
,因为无论你去哪里都可以认为是0
show/use它。