Search code examples
javascriptobjectforeach-loop-container

How to loop through a javascript object and sum amounts in a new object


I have a javascript object

const data = {
  type: "FeatureCollection",
  features: [{
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-118.4158548, 33.9811315]
    },
    properties: {
      name: "John Smith",
      total_due: 1
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-122.4605458, 37.7286008]
    },
    properties: {
      name: "June Waters",
      total_due: 50
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-122.4120835, 37.7778926]
    },
    properties: {
      name: "John Smith",
      total_due: 2
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-118.3508279, 34.0701918]
    },
    properties: {
      name: "June Waters",
      total_due: 60
    }
  }, {
    type: "Feature",
    geometry: {
      type: "Point",
      coordinates: [-82.4000143, 34.8518747]
    },
    properties: {
      name: "Mike Jones",
      total_due: 30
    }
  }]
};

What I'd like to do is loop through the object, add the unique names to a new object and sum the totals. The new object would look like this:

newObject = {'June Waters':110, 'Mike Jones':30, 'John Smith': 3}

I tried to do the following but I'm so new to JS so I don't know what I'm doing.

var newObject = {}
  
data.features.forEach(function (item) {
  newObject.item.properties.name = newObject.item.properties.name + item.properties.total_due
});

The following is a portion of the error that is generated.

TypeError: Cannot read properties of undefined (reading 'properties')
    at c:\Users\ryanj\z\const data =.js:10:18
    at Array.forEach (<anonymous>)....

Any help or hints would be truly appreciated.


Solution

  • you ran into an error because newObject.item is undefined since empty object therefore any property you are trying to access beyond that such as properties is not found

    by doing some tweaks to your implementation. I'm adding the name to the newObject if it already doesn't exist then i proceed to add the totals

    const data = {"type": "FeatureCollection","features": [{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.4158548, 33.9811315]},"properties": {"name": "John Smith","total_due": 1}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-122.4605458, 37.7286008]},"properties": {"name": "June Waters","total_due": 50}},{"type":"Feature","geometry": {"type": "Point","coordinates": [-122.4120835, 37.7778926]},"properties": {"name": "John Smith","total_due": 2}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.3508279, 34.0701918]},"properties": {"name": "June Waters","total_due": 60}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-82.4000143, 34.8518747]},"properties": {"name": "Mike Jones","total_due": 30}}]};
    
    var newObject = {}
      
    data.features.forEach(function (item) {
        if(!newObject[item.properties.name])newObject[item.properties.name] = 0;
      newObject[item.properties.name] = newObject[item.properties.name] + item.properties.total_due
    });
    
    console.log(newObject)

    similarly you can use reduce for these type of problems

    const data = {"type": "FeatureCollection","features": [{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.4158548, 33.9811315]},"properties": {"name": "John Smith","total_due": 1}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-122.4605458, 37.7286008]},"properties": {"name": "June Waters","total_due": 50}},{"type":"Feature","geometry": {"type": "Point","coordinates": [-122.4120835, 37.7778926]},"properties": {"name": "John Smith","total_due": 2}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-118.3508279, 34.0701918]},"properties": {"name": "June Waters","total_due": 60}},{"type": "Feature","geometry": {"type": "Point","coordinates": [-82.4000143, 34.8518747]},"properties": {"name": "Mike Jones","total_due": 30}}]};
    
    let total = data.features.reduce((acc,curr) =>{
        if(!acc[curr.properties.name])acc[curr.properties.name]=0;
      acc[curr.properties.name]+=curr.properties.total_due;
      return acc;
    },{})
    
    console.log(total)