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?

2

6 Answers 6

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

Sign up to request clarification or add additional context in comments.

Comments

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.

2 Comments

I get the following error when running this code: "undefined is not a function". On the line "for(values of obj).
@user7431590 either obj is not included correctly or youre using an old browser
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

Comments

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
}

Comments

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

Comments

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/[email protected]"></script>

Disclaimer: I'm the author of object-scan

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.