TransWikia.com

How to get the sum of the values of the JSON Array?

Stack Overflow Asked by iri0021 on December 27, 2021

I have below the Array that came from the JSON response

{
    "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

I needed the sum of the ‘chats and ‘missedChats’ per website id. I tried using Array.prototype.reduce() like this

const chatData =   fetch('https://bitbucket.org/!api/2.0/snippets/tawkto/aA8zqE/4f62624a75da6d1b8dd7f70e53af8d36a1603910/files/webstats.json');
var sum = JSON.parse(chatData).reduce(function(acc, val){
  return acc.chats + val.missedChats;
}, {chats, missedChats: 0});

but I get an error like this

VM362:1 Uncaught SyntaxError: Unexpected token o in JSON at position 1
at JSON.parse (<anonymous>)
at <anonymous>:4:16

Fairly new to JavaScript so any help will be appreciated, thanks.

4 Answers

try this

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

let group = data.reduce((r, a) => {
 r[a.websiteId] = [...r[a.websiteId] || [], {"missedChats":a.missedChats,"chats":a.chats}];
 return r;
}, {});

console.log(group);

Answered by dpmemcry on December 27, 2021

fetch() returns promise. Then parse it into json

const chatData = fetch('https://bitbucket.org/!api/2.0/snippets/tawkto/aA8zqE/4f62624a75da6d1b8dd7f70e53af8d36a1603910/files/webstats.json');

chatData.then(response => {
  return response.json();
}).then(people => {
  //process here
  });
});

Answered by xMayank on December 27, 2021

The fetch() return a Promise that you need to wait before getting data:

fetch('https://bitbucket.org/!api/2.0/snippets/tawkto/aA8zqE/4f62624a75da6d1b8dd7f70e53af8d36a1603910/files/webstats.json')
    .then(response => {
        response.text().then(data => {
            const json = JSON.parse(data);
            // Process data as json here
        });
    });

Answered by Eldynn on December 27, 2021

You can use an object with keys as websiteIds and add up the values using array.reduce like below:

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



let result = data.reduce(function(acc, val){
  if(!acc[val.websiteId]){
     acc[val.websiteId] = { chats: 0, missedChats: 0 };
  }
  acc[val.websiteId].chats += val.chats;
  acc[val.websiteId].missedChats += val.missedChats;
  
  return acc;
}, {});

console.log(result);

Answered by mickl on December 27, 2021

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP