I have a big JSON file (40 MB) that contains information about countries, their ids and finally a sum that I need to get its total.
{
"0": {
"id": 0,
"country": "usa",
"sum": 201,
},
"1": {
"id": 1,
"country": "fr",
"sum": 133,
}
}
When I try to fetch this data into a variable it works good but consumes so much memory, so I want to read these country objects' only sum field and then calculate the total sum. How can I do that?
I'm using following code to get the whole JSON:
fetch(fetchURL)
.then(response => response.json())
.then(responseJSON => {
this.json_data = responseJSON;
})
.catch(error => {
console.log(error);
});
responseJSON.sum
does not work, I think it requires index but I couldn't do it in the fetch.
map
,reduce
andforEach
when you work with arrays instead of loops usingfor
– Boussadjra Brahim Dec 27 '18 at 15:34