1

I have an object that looks like this:

{
  "Boiler Emissions": {
                        "Manhattan": 2.7,
                        "Bronx": 3.2
                      },
  "Benzene Concentration": {
                             "Manhattan": 2.1,
                             "Bronx": 3.5
                           }
}

And I want to count the values of Manhattan and Bronx from both sub objects and get a result that looks like this:

{
 "Manhattan": 4,8,
 "Bronx": 6,7
}

How do I count these nested values?

Nik So
  • 16,683
  • 21
  • 74
  • 108
user7431590
  • 91
  • 1
  • 7
  • There is no JSON in this question. You are talking about Javascript objects. Read: https://stackoverflow.com/questions/2904131/what-is-the-difference-between-json-and-object-literal-notation – Tomalak Jun 21 '17 at 08:51
  • Related to: https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object – Samuil Petrov Jun 21 '17 at 08:52

6 Answers6

1

You just need to loop over the keys of your object using Object.keys() and construct your result object.

This is how should be your code:

var result = {Manhattan: 0, Bronx: 0};
Object.keys(obj).forEach(function(k){
        result.Manhattan += obj[k].Manhattan ? obj[k].Manhattan : 0;
        result.Bronx+= obj[k].Bronx ? obj[k].Bronx: 0;
});

Demo:

This is a working Demo:

var obj = {
  "Boiler Emissions": {
    "Manhattan": 2.7,
    "Bronx": 3.2
  },
  "Benzene Concentration": {
    "Manhattan": 2,
    "Bronx": 3.5
  }
};

var result = {Manhattan: 0, Bronx: 0};
Object.keys(obj).forEach(function(k){
        result.Manhattan += obj[k].Manhattan ? obj[k].Manhattan : 0;
        result.Bronx+= obj[k].Bronx ? obj[k].Bronx: 0;
});

console.log(result);
cнŝdk
  • 31,391
  • 7
  • 56
  • 78
0
var obj={
    "Boiler Emissions": {
        "Manhattan": 2.7,
        "Bronx": 3.2
    },
    "Benzene Concentration": {
        "Manhattan": 2,1,
        "Bronx": 3.5
    }
};
var sums={};
for(values of obj){
 for(key in values){
  sums[key]= (sums[key]|| 0)  + values[key];
 }
}

console.log(sums);

Simply iterate over the main object.

Jonas Wilms
  • 132,000
  • 20
  • 149
  • 151
0

you need use filter array method.

 function filterByID(item) {

            if (item.CategoryID == viewCategoryProductID) {
                return true;
            }
            return false;
        }

this is a filter array method you need to pass items of the array and match value and it returns only matching values.

I hope it helps you

Neeraj Pathak
  • 759
  • 4
  • 13
0
const obj = {
  "Boiler Emissions": {
                        "Manhattan": 2.7,
                        "Bronx": 3.2
                      },
  "Benzene Concentration": {
                             "Manhattan": 2.1,
                             "Bronx": 3.5
                           }
}

const output = Object.keys(obj)
  .map(measure => obj[measure])
  .reduce(
    (sumByCity, measure) => {
      Object
      .keys(measure)
      .map( city => sumByCity[city] = !sumByCity[city] ? measure[city] : sumByCity[city] + measure[city] )
      return sumByCity  
    }


  ,{})
console.log(output)

webpackbin demo

The code has no mention of any of the measurements, so you can add as many as you want. Same thing goes on for cities, in case if you ever have to add more cities. And not all measurements need to have all cities. So if you have something like this:

const obj = {
  "Boiler Emissions": {
                        "Manhattan": 2.7,
                        "Bronx": 3.2,
                        "San Francisco": 3.2
                      },
  "Benzene Concentration": {
                             "Manhattan": 2.1,
                             "Bronx": 3.5,
                              "LA": 1.1
                           }
}

it will still output the correct result:

{
  Manhattan: 4.800000000000001,
  Bronx: 6.7,
  San Francisco: 3.2,
  LA: 1.1
}
Nik So
  • 16,683
  • 21
  • 74
  • 108
0

You may do as follows;

var obj = { "Boiler Emissions"     : {"Manhattan": 2.7, "Bronx": 3.2},
            "Benzene Concentration": {"Manhattan": 2.1, "Bronx": 3.5}},
    res = Object.keys(obj).reduce((r,k) => Object.keys(obj[k]).reduce((o,p) => (o[p] = o[p] + obj[k][p] || obj[k][p], o), r), {});
console.log(res);
Redu
  • 25,060
  • 6
  • 56
  • 76
0

We started using object-scan for data processing tasks like this. It's pretty powerful once you wrap your head around it. Here is how you'd answer your questions

// const objectScan = require('object-scan');

const count = (input) => objectScan(['**'], {
  filterFn: ({ value, property, context }) => {
    if (typeof value === 'number') {
      context[property] = (context[property] || 0) + value;
    }
  }
})(input, {});

const data = { 'Boiler Emissions': { Manhattan: 2.7, Bronx: 3.2 }, 'Benzene Concentration': { Manhattan: 2.1, Bronx: 3.5 } };

console.log(count(data));
// => { Bronx: 6.7, Manhattan: 4.800000000000001 }
.as-console-wrapper {max-height: 100% !important; top: 0}
<script src="https://bundle.run/object-scan@13.8.0"></script>

Disclaimer: I'm the author of object-scan

vincent
  • 1,953
  • 3
  • 18
  • 24