简体   繁体   中英

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

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.

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
        });
    });

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
  });
});

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);

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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM