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.