英文:
NodeJS calculate its sum for specific date from JSON
问题
I want to count how many sales each ID made in between starting date beginning 2021 January until end date December 2023. The result would be somehow like this
[{
"ID": "01",
"totalsales": 150
},
.
.
.
{
"ID": "15",
"totalsales": 300
}]
英文:
I am new in NodeJS and I am learning on how to calculate the data,
Consider the JSON data from the url is like this and there are about 100 data.
[{
"ID": "01",
"sales": 11,
"points": 5,
"date": "2019-04-23T18:25:43.511Z"
},
{
"ID": "02",
"sales": 25,
"points": 15,
"date": "2022-05-23T18:25:43.511Z"
},
.
.
.
{
"ID": "12",
"sales": 20,
"points": 11,
"date": "2022-05-23T18:25:43.511Z"
},
{
"ID": "01",
"sales": 50,
"points": 30,
"date": "2023-11-23T18:25:43.511Z",
},
{
"ID": "01",
"sales": 40,
"points": 30,
"date": "2021-07-23T18:25:43.511Z",
}]
I want to count how many sales each ID made in between starting date beginning 2021 January until end date December 2023. The result would be somehow like this
[{
"ID": "01",
"totalsales": 150
},
.
.
.
{
"ID": "15",
"totalsales": 300
}]
答案1
得分: 1
你可以这样轻松完成:
const jsonData = [{
"ID": "01",
"sales": 11,
"points": 5,
"date": "2019-04-23T18:25:43.511Z"
},
{
"ID": "02",
"sales": 25,
"points": 15,
"date": "2022-05-23T18:25:43.511Z"
},
{
"ID": "12",
"sales": 20,
"points": 11,
"date": "2022-05-23T18:25:43.511Z"
},
{
"ID": "01",
"sales": 50,
"points": 30,
"date": "2023-11-23T18:25:43.511Z",
},
{
"ID": "01",
"sales": 40,
"points": 30,
"date": "2021-07-23T18:25:43.511Z",
}]
const startDate = new Date('2021-01-01T00:00:00.000Z');
const endDate = new Date('2024-01-01T00:00:00.000Z');
const result = {}; // 创建一个空对象来存储结果
jsonData.forEach((data) => {
const date = new Date(data.date);
const year = date.getUTCFullYear();
const id = data.ID;
if (date >= startDate && date <= endDate) { // 检查日期是否在指定范围内
if (result[id]) {
result[id].totalsales += data.sales; // 添加到现有ID的总销售额
} else {
result[id] = { ID: id, totalsales: data.sales }; // 使用销售数据创建新的ID条目
}
}
});
console.log(Object.values(result)); // 以对象数组的形式打印最终结果
这是提供的代码的翻译部分。
英文:
You can easily do it like this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const jsonData = [{
"ID": "01",
"sales": 11,
"points": 5,
"date": "2019-04-23T18:25:43.511Z"
},
{
"ID": "02",
"sales": 25,
"points": 15,
"date": "2022-05-23T18:25:43.511Z"
},
{
"ID": "12",
"sales": 20,
"points": 11,
"date": "2022-05-23T18:25:43.511Z"
},
{
"ID": "01",
"sales": 50,
"points": 30,
"date": "2023-11-23T18:25:43.511Z",
},
{
"ID": "01",
"sales": 40,
"points": 30,
"date": "2021-07-23T18:25:43.511Z",
}]
const startDate = new Date('2021-01-01T00:00:00.000Z');
const endDate = new Date('2024-01-01T00:00:00.000Z');
const result = {}; // Create an empty object to store the result
jsonData.forEach((data) => {
const date = new Date(data.date);
const year = date.getUTCFullYear();
const id = data.ID;
if (date >= startDate && date <= endDate) { // Check if date is within the specified range
if (result[id]) {
result[id].totalsales += data.sales; // Add to existing ID's total sales
} else {
result[id] = { ID: id, totalsales: data.sales }; // Create new ID entry with sales data
}
}
});
console.log(Object.values(result)); // Print the final result as an array of objects
<!-- end snippet -->
答案2
得分: 0
以下是代码的翻译部分:
const [startDate, endDate] = ["2021-01-01", "2022-01-01"] // 例如从2021年到2022年
// 结果是一个对象,其键是ID,值是它们在该时间段内的销售额
const salesById = data.reduce((sales, current) => {
const date = new Date(current.date);
if (date >= startDate && date <= endDate) {
const id = current.ID;
// 当前条目在时间范围内,将其添加到累加器对象中
sales[id] = (sales[id] || 0) + current.sales;
}
return sales; // 继续下一条数据条目,累积到目前为止的所有销售数据
}, {});
// 以所需的格式呈现结果
const formattedResult = Object.keys(salesById).map(ID => ({
ID,
totalsales: salesById[ID]
}));
请注意,这是您提供的代码的中文翻译部分。
英文:
You can do it pretty easily with the help of with the reduce
function:
const [startDate, endDate] = ["2021-01-01", "2022-01-01"] // eg from 2021 to 2022
// results in an object with the keys being the IDs and the values being their respective sales in the time-period
const salesById = data.reduce((sales, current) => {
const date = new Date(current.date);
if (date >= startDate && date <= endDate) {
const id = current.ID;
// the current entry is within the time-frame, add it to the accumulator object
sales[id] = (sales[id] || 0) + current.sales;
}
return sales; // continue with the next data entry with all sales-data accumulated so far
}, {});
// Results in your desired format
const formattedResult = Object.keys(salesById).map(ID => ({
ID,
totalsales: salesById[ID]
}));
答案3
得分: 0
你可以使用Array.reduce()
来按ID
对项目进行分组。这将创建一个具有每个ID属性的对象,然后我们可以使用Object.values()
将结果作为数组获取。
我们还会按日期进行筛选,检查每个日期是否在开始日期和结束日期之间。
const data = [{ "ID": "01", "sales": 11, "points": 5, "date": "2019-04-23T18:25:43.511Z" }, { "ID": "02", "sales": 25, "points": 15, "date": "2022-05-23T18:25:43.511Z" }, { "ID": "12", "sales": 20, "points": 11, "date": "2022-05-23T18:25:43.511Z" }, { "ID": "01", "sales": 50, "points": 30, "date": "2023-11-23T18:25:43.511Z", }, { "ID": "01", "sales": 40, "points": 30, "date": "2021-07-23T18:25:43.511Z", }]
const startDate = '2021-01-01T00:00:000Z';
const endDate = '2024-01-01T00:00:000Z';
const result = Object.values(data.reduce((acc, { ID, sales, date }) => {
if ((date >= startDate) && (date < endDate)) {
acc[ID] = acc[ID] || { ID, totalSales: 0 };
acc[ID].totalSales += sales;
}
return acc;
}, {}))
console.log('Total sales by ID:')
console.log(result.sort((a,b) => a.ID - b.ID));
请注意:上述代码是JavaScript代码,用于根据ID和日期对数据进行分组和筛选,然后计算每个ID的总销售额。
英文:
You can use Array.reduce()
to group the items by ID
.
This creates an object with a property for each ID, we can then use Object.values()
to get the result as an array.
We'll also filter by date, checking that each date is between the start and end dates.
<!-- begin snippet: js hide: false console: true babel: null -->
<!-- language: lang-js -->
const data = [{ "ID": "01", "sales": 11, "points": 5, "date": "2019-04-23T18:25:43.511Z" }, { "ID": "02", "sales": 25, "points": 15, "date": "2022-05-23T18:25:43.511Z" }, { "ID": "12", "sales": 20, "points": 11, "date": "2022-05-23T18:25:43.511Z" }, { "ID": "01", "sales": 50, "points": 30, "date": "2023-11-23T18:25:43.511Z", }, { "ID": "01", "sales": 40, "points": 30, "date": "2021-07-23T18:25:43.511Z", }]
const startDate = '2021-01-01T00:00:000Z';
const endDate = '2024-01-01T00:00:000Z';
const result = Object.values(data.reduce((acc, { ID, sales, date }) => {
if ((date >= startDate) && (date < endDate)) {
acc[ID] = acc[ID] || { ID, totalSales: 0 };
acc[ID].totalSales += sales;
}
return acc;
}, {}))
console.log('Total sales by ID:')
console.log(result.sort((a,b) => a.ID - b.ID));
<!-- language: lang-css -->
.as-console-wrapper { max-height: 100% !important; }
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论