Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Map and Reduce JSON Objects with JavaScript

Tags:

Consider this JSON object below:

{
   "cells":[
      {
         "count":"1",
         "gdp_growth__avg":1.90575802503285,
         "geo__name":"united states of america",
         "time":1990
      },
      {
         "count":"1",
         "gdp_growth__avg":9.17893670154459,
         "geo__name":"china",
         "time":1991
      },
      {
         "count":"1",
         "gdp_growth__avg":-5.04693945214571,
         "geo__name":"russia",
         "time":1991
      },
      {
         "count":"1",
         "gdp_growth__avg":-0.0622142217811472,
         "geo__name":"botswana",
         "time":1991
      },
      {
         "count":"1",
         "gdp_growth__avg":14.2407063986337,
         "geo__name":"china",
         "time":1992
      },
      {
         "count":"1",
         "gdp_growth__avg":-14.5310737731921,
         "geo__name":"russia",
         "time":1992
      },
      {
         "count":"1",
         "gdp_growth__avg":3.55494453739944,
         "geo__name":"united states of america",
         "time":1992
      },
      {
         "count":"1",
         "gdp_growth__avg":13.9643147001603,
         "geo__name":"china",
         "time":1993
      },
      {
         "count":"1",
         "gdp_growth__avg":-8.66854034194856,
         "geo__name":"botswana",
         "time":1993
      },
      {
         "count":"1",
         "gdp_growth__avg":2.74204850437989,
         "geo__name":"united states of america",
         "time":1993
      },
      {
         "count":"1",
         "gdp_growth__avg":4.04272516401846,
         "geo__name":"united states of america",
         "time":1994
      },
      {
         "count":"1",
         "gdp_growth__avg":13.0806818010789,
         "geo__name":"china",
         "time":1994
      },
      {
         "count":"1",
         "gdp_growth__avg":-12.5697559787493,
         "geo__name":"russia",
         "time":1994
      },
      {
         "count":"1",
         "gdp_growth__avg":10.9249803004994,
         "geo__name":"china",
         "time":1995
      },
      {
         "count":"1",
         "gdp_growth__avg":-4.14352840666389,
         "geo__name":"russia",
         "time":1995
      },
      {
         "count":"1",
         "gdp_growth__avg":2.71655384149574,
         "geo__name":"united states of america",
         "time":1995
      },
      {
         "count":"1",
         "gdp_growth__avg":10.0085233990531,
         "geo__name":"china",
         "time":1996
      },
      {
         "count":"1",
         "gdp_growth__avg":3.79848988541973,
         "geo__name":"united states of america",
         "time":1996
      }
]
}

I would to Map and Reduce and generate a new object containing a summation of the GDP growth for all the countries in the above JSON that might look roughly like this:

{  
  {
     "gdp_growth__avg":46.23,
     "geo__name":"united states of america",
  },
  {
     "gdp_growth__avg":16.23,
     "geo__name":"china",
  },
  {
     "gdp_growth__avg":36.23,
     "geo__name":"russia",
  },
  {
     "gdp_growth__avg":26.23, 
     "geo__name":"botswana",
     "time":1991
  }
 }

I have looked at map and reduce and am not sure how best to proceed.

I was thinking something like this might be moving in the right directions, but does not seem to do what I was expecting:

      var arr = [{x:1},{x:2},{x:4}];

      arr.reduce(function (a, b) {
        return {x: a.x + b.x};
      });

      console.log(arr); //Outputs that same initial array

While I recognize that it is probably better and easier to do this on the server-side, I am wondering if what I am trying to do can be done on the client side with JavaScript. Any suggestions? Thanks in advance.

like image 730
Mr. Concolato Avatar asked Dec 01 '15 00:12

Mr. Concolato


People also ask

How to use reduce on JSON in JavaScript?

1st ExampleThe map() method followed by the reduce() method. The map() extracts all marks from the JSON and stores the data it in an array. The reduce() runs a callback function named sum() and returns the total marks obtained. 👉 You can write the above code using Arrow function.

Does map work on JSON?

You can map the data types of your business model into JSON by using the examples. Data in JSON is either an object or an array. A JSON object is an unordered collection of names and values. A JSON array is an ordered sequence of values.

How do I reduce the size of a JSON file?

As text data, JSON data compresses nicely. That's why gzip is our first option to reduce the JSON data size. Moreover, it can be automatically applied in HTTP, the common protocol for sending and receiving JSON.

Is a map a JSON object?

A JSONObject is an unordered collection of name/value pairs whereas Map is an object that maps keys to values. A Map cannot contain duplicate keys and each key can map to at most one value. We need to use the JSON-lib library for serializing and de-serializing a Map in JSON format.


1 Answers

Try this:

var data = { cells:[...] };

var r = data.cells.reduce(function(pv, cv) {
    if ( pv[cv.geo__name] ) {
        pv[cv.geo__name] += cv.gdp_growth__avg;
    } else {
        pv[cv.geo__name] = cv.gdp_growth__avg;
    }
    return pv;
}, {});

console.log(r);

Output example:

    { 
      'united states of america': 18.76051995774611,
      'china': 71.39814330096999,
      'russia': -36.291297610751,
      'botswana': -8.730754563729707 
   }
like image 168
marsgpl Avatar answered Oct 06 '22 12:10

marsgpl