2021年4月24日星期六

Count the occurrence of success and failed based on the status of each datapoint

Given an array of datapoints. I want to be able to count the occurrence of a datapoint x by the status of each point. The datapoint can exist more than once. The goal is to keep a count for each point and finally return an object of each point and the failure and successful count

For example. Given an array

const data = [          {              "x": 14,              "y": "1",              "created_at": "2021-04-14T14:23:59.825Z",              "status": "undelivered"          },          {              "x": 14,              "y": "1",              "created_at": "2021-04-14T14:27:51.666Z",              "status": "undelivered"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T10:34:07.317Z",              "status": "success"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T10:34:08.601Z",              "status": "undelivered"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T10:34:09.871Z",              "status": "undelivered"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T10:34:11.108Z",              "status": "undelivered"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T10:35:17.969Z",              "status": "pending"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T11:10:59.600Z",              "status": "pending"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T11:22:29.020Z",              "status": "pending"          },          {              "x": 15,              "y": "1",              "created_at": "2021-04-15T11:48:15.974Z",              "status": "pending"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T09:56:30.156Z",              "status": "pending"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T10:49:55.734Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T10:49:56.707Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T10:49:57.692Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T10:49:58.620Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T10:49:59.638Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T10:50:00.561Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T11:11:13.602Z",              "status": "pending"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T11:11:41.271Z",              "status": "pending"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T11:12:06.548Z",              "status": "pending"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T11:26:02.834Z",              "status": "pending"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T11:27:05.462Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T12:11:21.446Z",              "status": "undefined"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T12:12:18.397Z",              "status": "undelivered"          },          {              "x": 16,              "y": "1",              "created_at": "2021-04-16T12:22:05.958Z",              "status": "accepted"          },          {              "x": 18,              "y": "1",              "created_at": "2021-04-18T18:06:57.946Z",              "status": "undelivered"          },          {              "x": 18,              "y": "1",              "created_at": "2021-04-18T18:06:59.179Z",              "status": "undelivered"          },          {              "x": 18,              "y": "1",              "created_at": "2021-04-18T18:07:00.375Z",              "status": "undelivered"          },          {              "x": 18,              "y": "1",              "created_at": "2021-04-18T18:07:01.534Z",              "status": "undelivered"          },          {              "x": 18,              "y": "1",              "created_at": "2021-04-18T18:07:02.633Z",              "status": "undelivered"          },          {              "x": 18,              "y": "1",              "created_at": "2021-04-18T18:07:03.732Z",              "status": "undelivered"          },          {              "x": 20,              "y": "1",              "created_at": "2021-04-20T07:56:17.967Z",              "status": "undelivered"          },          {              "x": 20,              "y": "1",              "created_at": "2021-04-20T07:56:19.073Z",              "status": "undelivered"          },          {              "x": 20,              "y": "1",              "created_at": "2021-04-20T07:56:20.000Z",              "status": "undelivered"          },          {              "x": 20,              "y": "1",              "created_at": "2021-04-20T07:56:21.008Z",              "status": "undelivered"          },          {              "x": 20,              "y": "1",              "created_at": "2021-04-20T07:56:21.972Z",              "status": "undelivered"          },          {              "x": 20,              "y": "1",              "created_at": "2021-04-20T07:56:22.946Z",              "status": "undelivered"          }      ]  

I want to be able to get result as

result = {     14: {       failureCount: 2,       successCount : 0,       total: 2     },     15: {       failureCount: 7,       successCount : 1,       total: 8     }     .......  }  

failureCount is incremented when status is not equal to delivered or success i.e

element.status !== "delivered" || element.status !== "success"    where element is the item in each array  

successCount is incremented when status is equal to delivered or success i.e

element.status == "delivered" || element.status == "success"    where element is the item in each array  

total is the summation of the y datapoint

Attempt

const map = {};  let failureCount = 0;  let successCount = 0;  data.forEach((element, index) => {    map[element.x] = {      failureCount: failureCount,      successCount: successCount    }    const failed = element.status !== "delivered" || element.status !== "success"    if(element.x in map && failed){      map[element.x] = {        failureCount: map[element.x].failureCount + 1      }    }else {      map[element.x] = {        successCount: map[element.x].successCount + 1      }    }  })  
https://stackoverflow.com/questions/67240413/count-the-occurrence-of-success-and-failed-based-on-the-status-of-each-datapoint April 24, 2021 at 03:38PM

没有评论:

发表评论