Map and Reduce JSON Objects with JavaScript

javascript reduce
javascript map
javascript map, reduce
javascript filter array of objects by key
javascript map nested array of objects
javascript filter nested array of objects
javascript aggregate array of objects
map, filter reduce javascript freecodecamp

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.

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 
   }

How to simplify your codebase with map(), reduce(), and filter() in , Well, this is the only one not about map/reduce/filter, but it's so compact that it was The filter() method creates a new array with all elements that pass the test with selected keys as a string, but you realize that JSON.stringify is not that great ? How to get started with internationalization in JavaScript Map and Reduce JSON Objects with JavaScript. Ask Question Asked 4 years, 6 months ago. map and reduce can be used server side too if you use node. Probably

The Array.reduce method doesn't change the array object, it returns the results in a new array.

Iterating Over and Reducing Data, By default, the clone function will not copy over nested objects. JavaScript's map can be a very useful tool to implement this concept of a data.sort(function(​a,b) { return b.population - a.population; }); console.log(JSON.stringify(data)); Simplify the way you write your JavaScript by using .map(), .reduce() and .filter() instead of for() and forEach() loops. You’ll end up with clearer, less clunky code!

You can try something like this:

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

data.cells.map(function(datum) {
  return {
    geo__name: datum.geo__name,
    gdp_growth__avg: data.cells.filter(function(o) {
      return o.geo__name === datum.geo__name;
    }).reduce(function(sum, o) {
      return sum + o.gdp_growth__avg;
    }, 0)
  };
})

Needless to say, you can extract other properties from datum as well, such as time. I haven't.

Examples of Map, Filter and Reduce in JavaScript – Tania Rascia, js and make a JSON file with an object containing an array containing objects of all my coffee house locations. It can be a bit tricky to keep track of  Using reduce() to count instances of values in an object. reduce() is written like map() and filter(), but the inner function will take two parameters. It also has an extra spot at the end where we'll insert an empty object {}.

I tried to do with better time complexity:

let ans = [];
let map = new Map();
ques.cells.forEach(x => {
if(map[x.geo__name]){
map.set(x.geo__name, map[x.geo__name] + x.gdp_growth__avg);
}else{
map.set(x.geo__name,x.gdp_growth__avg);
}
});

map.forEach((v,k)=>{
ans.push({"geo__name":k, "gdp_growth__avg":v});
});
ques.cells = ans;

JavaScript ES6 Map and Reduce Patterns, Aron Racho·Aug 29, 2017javascript·map·reduce·patterns·es6 of objects and boil it down to a non-array structure (either a primitive or some JSON object). How to simplify your codebase with map(), reduce(), and filter() in JavaScript Photo by Anders Jildén on Unsplash When you read about Array.reduce and how cool it is, the first and sometimes the only example you find is the sum of numbers.

Map and Reduce JSON Objects with JavaScript, javascript aggregate array of objects javascript filter nested array of objects map, filter reduce javascript freecodecamp. Consider this JSON object below: Likewise, I use the reduce function if I find it necessary to take an array of objects and boil it down to a non-array structure (either a primitive or some JSON object). A large portion of the rest of this blogpost are examples of variations on these two themes.

Array.prototype.reduce(), The reduce() method executes a reducer function (that you provide) on each { x​: 2 } [ ].reduce( maxCallback ); // TypeError // map & reduce with need to support truly obsolete JavaScript engines that do not support Object. map returns an array with the same length, filter as the name implies, it returns an array with less items than the original array reduce returns a single value (or object) find returns the first items in an array that satisfies a condition

JavaScript: How to construct an array of json objects using map, How does JSON object look like? Answer: JSON objects are written as key/ value pairs. Example: var Person = { "name" : "Amy", "  The map() method creates a new array with the results of calling a function for every array element. The map() method calls the provided function once for each element in an array, in order. Note: map() does not execute the function for array elements without values. Note: this method does not change the original array.

Comments
  • You want console.log(arr.reduce(…)). reduce does not alter the array (or even reassign the variable), it just returns a value.
  • @Bergi, +1 because i learned something here that i did not expect. Thank you.
  • map and reduce can be used server side too if you use node. Probably whatever your server can run can do it. Most convenient in js or python (are there python-based servers?)
  • No, it does not return a new array.
  • it depends on the type of accumulator we use, it can be json object or even a js Map ,but array.map() returns a new array