如何在 Javascript 中按日期过滤器对数据求和?
How to sum data by date filter in Javascript?
我试图找到一种解决方案,以在对象中使用 ISO 日期信息对成本数据求和(按天分组)。
我的数据对象看起来像:
0: {id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'}
1: {id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'}
2: {id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'}
3: {id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'}
4: {id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-04T23:00:00.000Z'}
预期结果:
sumData = [222,50,30]
- 2022-02-05 的总费用 = 222
- 2022-02-06 的总费用 = 50
- 2022-02-09 的总费用 = 30
我试着用类似的东西来总结:
let currentDate = '';
let currentTotal = 0;
let sumData = [];
if(data) {
for (let index = 0; index < data.length; index++) {
//init date to compare with
if(currentDate === '') {
currentDate = new Date(data[index].date);
currentTotal = data[index].cost;
}
else {
const nextDate = new Date(data[index].date);
//test if is same day, sum expenses and add in sumData array
if(currentDate.getFullYear() === nextDate.getFullYear() &&
currentDate.getMonth() === nextDate.getMonth() &&
currentDate.getDate() === nextDate.getDate())
{
console.log('DATE = ' + currentDate.getDate());
console.log(data[index].date);
console.log('COST = ' + data[index].cost);
console.log('currentTotal = ' + currentTotal);
currentTotal = currentTotal + data[index].cost;
console.log(index + ' - COST = ' + data[index].cost);
console.log('TOTAL = ' + currentTotal);
console.log('===========');
}
else {
//push last valid sum before reset
currentTotal = 0;
//reset sum with last data
currentTotal = data[index].cost;
}
currentDate = new Date(data[index].date);
}
}
}
console.log('== SUM data ==');
sumData.push(currentTotal);
你可以这样做。
请注意,我更新了您的最后日期,因为它是 2022-02-04
而不是您问题中所说的 2022-02-05
。
var array = [
{id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'},
{id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'},
{id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'},
{id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'},
{id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-05T23:00:00.000Z'},
];
var result = [];
array.reduce(function(res, value) {
let onlyDate = value.date.split('T')[0];
// Second approach is just to take first 10 characters of a substring like this:
// let onlyDate = value.date.substring(0, 10);
if (!res[onlyDate]) {
res[onlyDate] = { date: onlyDate, cost: 0 };
result.push(res[onlyDate])
}
res[onlyDate].cost += value.cost;
return res;
}, {});
console.log("Result in format date/cost");
console.log(result);
const onlyCostsResult = result.map(item => item.cost);
console.log("Only costs");
console.log(onlyCostsResult);
您可以尝试使用.reduce() and Object.values()来达到您的目的:
const data = [
{id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'},
{id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'},
{id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'},
{id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'},
{id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-04T23:00:00.000Z'}
]
const res = Object.values(data.reduce((resMap, obj) => {
const date = new Date(obj.date).toLocaleString().split(',')[0]
if (resMap[date] !== undefined)
resMap[date] += obj.cost
else
resMap[date] = obj.cost
return resMap;
}, {}))
console.log(res)
更新:使用 ISO8601 日期
我试图找到一种解决方案,以在对象中使用 ISO 日期信息对成本数据求和(按天分组)。
我的数据对象看起来像:
0: {id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'}
1: {id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'}
2: {id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'}
3: {id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'}
4: {id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-04T23:00:00.000Z'}
预期结果:
sumData = [222,50,30]
- 2022-02-05 的总费用 = 222
- 2022-02-06 的总费用 = 50
- 2022-02-09 的总费用 = 30
我试着用类似的东西来总结:
let currentDate = '';
let currentTotal = 0;
let sumData = [];
if(data) {
for (let index = 0; index < data.length; index++) {
//init date to compare with
if(currentDate === '') {
currentDate = new Date(data[index].date);
currentTotal = data[index].cost;
}
else {
const nextDate = new Date(data[index].date);
//test if is same day, sum expenses and add in sumData array
if(currentDate.getFullYear() === nextDate.getFullYear() &&
currentDate.getMonth() === nextDate.getMonth() &&
currentDate.getDate() === nextDate.getDate())
{
console.log('DATE = ' + currentDate.getDate());
console.log(data[index].date);
console.log('COST = ' + data[index].cost);
console.log('currentTotal = ' + currentTotal);
currentTotal = currentTotal + data[index].cost;
console.log(index + ' - COST = ' + data[index].cost);
console.log('TOTAL = ' + currentTotal);
console.log('===========');
}
else {
//push last valid sum before reset
currentTotal = 0;
//reset sum with last data
currentTotal = data[index].cost;
}
currentDate = new Date(data[index].date);
}
}
}
console.log('== SUM data ==');
sumData.push(currentTotal);
你可以这样做。
请注意,我更新了您的最后日期,因为它是 2022-02-04
而不是您问题中所说的 2022-02-05
。
var array = [
{id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'},
{id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'},
{id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'},
{id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'},
{id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-05T23:00:00.000Z'},
];
var result = [];
array.reduce(function(res, value) {
let onlyDate = value.date.split('T')[0];
// Second approach is just to take first 10 characters of a substring like this:
// let onlyDate = value.date.substring(0, 10);
if (!res[onlyDate]) {
res[onlyDate] = { date: onlyDate, cost: 0 };
result.push(res[onlyDate])
}
res[onlyDate].cost += value.cost;
return res;
}, {});
console.log("Result in format date/cost");
console.log(result);
const onlyCostsResult = result.map(item => item.cost);
console.log("Only costs");
console.log(onlyCostsResult);
您可以尝试使用.reduce() and Object.values()来达到您的目的:
const data = [
{id: 45, title: 'abo', cost: 10, recurrent: true, date: '2022-02-09T20:43:30.139Z'},
{id: 123, title: 'test 4', cost: 20, recurrent: false, date: '2022-02-09T20:43:30.139Z'},
{id: 125, title: 'test 3', cost: 50, recurrent: false, date: '2022-02-06T23:00:00.000Z'},
{id: 125, title: 'test 2', cost: 100, recurrent: false, date: '2022-02-05T10:00:00.000Z'},
{id: 125, title: 'test 1', cost: 122, recurrent: false, date: '2022-02-04T23:00:00.000Z'}
]
const res = Object.values(data.reduce((resMap, obj) => {
const date = new Date(obj.date).toLocaleString().split(',')[0]
if (resMap[date] !== undefined)
resMap[date] += obj.cost
else
resMap[date] = obj.cost
return resMap;
}, {}))
console.log(res)
更新:使用 ISO8601 日期