Get total chat and missedChats

I have a sample data like this.

{
    "websiteId": "4f8b36d00000000000000001",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 121,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000002",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 13,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000003",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 232,
    "missedChats": 9
},
{
    "websiteId": "4f8b36d00000000000000004",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 9,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000005",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000008",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
}

My code:

const dataset = [];

const websiteIds = [];
const dates = [];
const chats = [];
const missedChats = [];

let chatsSum = 0;
let missedChatsSum = 0;

function getData() {
  if (start == null && end == null) {
    fetch(url)
    .then(res => res.json())
    .then(data => {
      data.map(result => {
        console.log(result)
        let sets = {
            websiteId: result.websiteId,
            chats: result.chats,
            missedChats: result.chats,
          }
    
    chats.push(result.chats)
    missedChats.push(result.missedChats)
    
    if (!websiteIds.includes(result.websiteId)) {
      websiteIds.push(result.websiteId)
      for (let x = 0; x < websiteIds.length; x++) {
        if (sets.websiteId == websiteIds[x]) {
          dataset.push(sets)
        }
        if (result.websiteId == websiteIds[x]) {
          dataset[x].chats += result.chats
          dataset[x].missedChats += result.missedChats
        }
      }
    }
  })
})


 } else {
    console.log('date given')
  }
}

My expected output should be like this

{
    "websiteId": "4f8b36d00000000000000001",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 500, // total number of chat in this websiteID
    "missedChats": 15 // total number of chat in this websiteID
},
{
    "websiteId": "4f8b36d00000000000000002",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 100, // total number of chat in this websiteID
    "missedChats": 50// total number of chat in this websiteID
},
{
    "websiteId": "4f8b36d00000000000000003",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 200, // total number of chat in this websiteID
    "missedChats": 25 // total number of chat in this websiteID
}

I’m trying to get the total sum of the chat and missedchats per my websiteID. Is there a way on how I can get the data on the given example? When I’m trying to run my code it doesn’t sums up the chat and misschats it only gets the first value per websiteID.

Answer

You can use reduce to sum the values:

function sum(data) {
    return Object.values(data.reduce((acc, el) => {
        if (!(el.websiteId in acc)) {
            acc[el.websiteId] = el;
        } else {
            acc[el.websiteId].chats += el.chats;
            acc[el.websiteId].missedChats += el.missedChats;
        }
        return acc;
    }, {}));
}


const data = [{
    "websiteId": "4f8b36d00000000000000001",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 121,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000002",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 13,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000003",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 232,
    "missedChats": 9
},
{
    "websiteId": "4f8b36d00000000000000004",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 9,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000005",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000008",
    "date": "2019-04-01T00:00:00.000Z",
    "chats": 0,
    "missedChats": 0
},
{
    "websiteId": "4f8b36d00000000000000007",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
},
{
    "websiteId": "4f8b36d00000000000000006",
    "date": "2019-04-02T00:00:00.000Z",
    "chats": 0,
    "missedChats": 1
}];

console.log(sum(data));

The block

data.reduce((acc, el) => {
    if (!(el.websiteId in acc)) {
        acc[el.websiteId] = el;
    } else {
        acc[el.websiteId].chats += el.chats;
        acc[el.websiteId].missedChats += el.missedChats;
    }
    return acc;
}, {})

creates an object using websiteId as key containing the summed values. Object.values returns the values as array.

Leave a Reply

Your email address will not be published. Required fields are marked *