skip to Main Content

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
}] 

3

Answers


  1. 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]
    }));
    
    
    Login or Signup to reply.
  2. 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.

    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));
    .as-console-wrapper { max-height: 100% !important; }
    Login or Signup to reply.
  3. You can easily do it like this:

    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
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search