user11795487
user11795487

Reputation:

How do I count the number of items in each day for a month from the json data using javascript

I want write a program to calculate the sum of some variables for a single day for all the data present in json response from a mysql database with the help of ajax calls.

In this data, I want to access the

variables in such a way that the date 2021-05-30 the sum of cylinderchecked will be 9 and the prodRate will be the average of the prodrate and cylinderchecked.

For example

let data = [{
    "0": "2021-05-31 09:10:27",
    "1": "1",
    "2": "1",
    "Date": "2021-05-31 09:10:27",
    "ProdRate": "1",
    "CheckedCylinders": "1"
  },
  {
    "0": "2021-05-30 22:03:32",
    "1": "1",
    "2": "4",
    "Date": "2021-05-30 22:03:32",
    "ProdRate": "1",
    "CheckedCylinders": "4"
  },
  {
    "0": "2021-05-30 18:41:34",
    "1": "1",
    "2": "9",
    "Date": "2021-05-30 18:41:34",
    "ProdRate": "1",
    "CheckedCylinders": "9"
  },
  {
    "0": "2021-05-30 18:32:28",
    "1": "1",
    "2": "8",
    "Date": "2021-05-30 18:32:28",
    "ProdRate": "1",
    "CheckedCylinders": "8"
  },
  {
    "0": "2021-05-30 09:20:19",
    "1": "1",
    "2": "8",
    "Date": "2021-05-30 09:20:19",
    "ProdRate": "1",
    "CheckedCylinders": "8"
  }
]

function report(result, dates) {
  var totalCy = [],
    prodRate = [],
    sum1 = 0,
    sum2 = 0;

  for (let x in result) {
    totalCy.push(parseInt(result[x].CheckedCylinders));
    prodRate.push(parseInt(result[x].ProdRate));
  }
  for (x in totalCy && prodRate) {
    sum1 = sum1 + totalCy[x];
    sum2 = sum2 + prodRate[x];
  }

  sum2 = sum2 / sum1;
  for (var x in dates) {
    $(".degassingReportbody").append(`
        <tr>
        <td>${dates[x]}</td>
        <td>${sum1}</td>
        <td>${sum2}</td>
        </tr>
        `);
  }
}

function degassingReport(data) {
  //DATE & TIME Total Cyl Checked   Production Rate

  $(".degassingReportHead").append(`
        <tr>
        <th>DATE & TIME</th>
        <th>Total Cyl Checked</th>
        <th>Production Rate</th>
        </tr>
        `);
  let date = [];
  for (let x in data) {
    date.push(data[x].Date.split(" ")[0]);
  }
  let newDate = new Set(date);
  let dates = [...newDate];

  for (var x in dates) {
    result = data.filter(function(obj) {
      return obj.Date.split(" ")[0] == dates[x];
    });
  }
  report(result, dates);
}

degassingReport(data)
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <thead class="degassingReportHead"></thead>
  <tbody class="degassingReportbody"></tbody>
</table>

Upvotes: 0

Views: 217

Answers (1)

Frenchy
Frenchy

Reputation: 17007

a solution using reduce:

let data = [{
    "0": "2021-05-31 09:10:27",
    "1": "1",
    "2": "1",
    "Date": "2021-05-31 09:10:27",
    "ProdRate": "1",
    "CheckedCylinders": "1"
  },
  {
    "0": "2021-05-30 22:03:32",
    "1": "1",
    "2": "4",
    "Date": "2021-05-30 22:03:32",
    "ProdRate": "1",
    "CheckedCylinders": "4"
  },
  {
    "0": "2021-05-30 18:41:34",
    "1": "1",
    "2": "9",
    "Date": "2021-05-30 18:41:34",
    "ProdRate": "1",
    "CheckedCylinders": "9"
  },
  {
    "0": "2021-05-30 18:32:28",
    "1": "1",
    "2": "8",
    "Date": "2021-05-30 18:32:28",
    "ProdRate": "1",
    "CheckedCylinders": "8"
  },
  {
    "0": "2021-05-30 09:20:19",
    "1": "1",
    "2": "8",
    "Date": "2021-05-30 09:20:19",
    "ProdRate": "1",
    "CheckedCylinders": "8"
  }
]

var groupBy = function(xs, key) {
  return xs.reduce(function(rv, x) {
     let k = x[key].split(" ")[0];
     if(rv[k]){
      rv[k][0] += +x.ProdRate;
      rv[k][1] += +x.CheckedCylinders;
      rv[k][2] += rv[k][0]/rv[k][1]; 
     }else{
      rv[k] = [+x.ProdRate,+x.CheckedCylinders, (+x.ProdRate)/(+x.CheckedCylinders)];
     }
    return rv;
  }, {});
};
var groubedByDay=groupBy(data, 'Date');

degassingReport(groubedByDay);


function degassingReport(datas) {
  //DATE & TIME Total Cyl Checked   Production Rate

  $(".degassingReportHead").append(`
        <tr>
        <th>DATE & TIME</th>
        <th>Total Cyl Checked</th>
        <th>Production Rate</th>
        </tr>
        `);

  for (var x in datas) {
    $(".degassingReportbody").append(`
        <tr>
        <td>${x}</td>
        <td>${datas[x][1]}</td>
        <td>${datas[x][2]}</td>
        </tr>
        `);
  }
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

<table>
  <thead class="degassingReportHead"></thead>
  <tbody class="degassingReportbody"></tbody>
</table>

Upvotes: 1

Related Questions