如何循环遍历 JavaScript 对象并对新对象中的金额求和
我有一个 javascript 对象,
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
}
}]
};
我想做的是循环访问该对象,将唯一名称添加到新对象中并对总数进行求和。新对象看起来像这样:
newObject = {'June Waters':110, 'Mike Jones':30, 'John Smith': 3}
我尝试执行以下操作,但我对 JS 很陌生,所以我不知道我在做什么。
var newObject = {}
data.features.forEach(function (item) {
newObject.item.properties.name = newObject.item.properties.name + item.properties.total_due
});
以下是生成的错误的一部分。
TypeError: Cannot read properties of undefined (reading 'properties')
at c:\Users\ryanj\z\const data =.js:10:18
at Array.forEach (<anonymous>)....
任何帮助或提示将不胜感激。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您遇到了错误,因为
newObject.item
是未定义
,因为空对象因此您尝试访问的任何属性(例如properties
)都不是 来发现通过对您的实现进行一些调整 。我将名称添加到
newObject
中(如果它不存在),然后我继续添加总计同样,您可以使用
reduce
来解决这些类型的问题you ran into an error because
newObject.item
isundefined
since empty object therefore any property you are trying to access beyond that such asproperties
is not foundby 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 totalssimilarly you can use
reduce
for these type of problems在 Javascript 中,您可以使用方括号
[]
引用对象属性,就像我们在数组中所做的那样。在这段代码中,我们首先检查对象中是否已经定义了键。如果未定义,我们将简单地将total_due 的值分配给键(在本例中是人名)。在下一次迭代中,如果 Javascript 识别出该键并且该键的值不是未定义的,我们只需将
total_due
值添加到当前键值即可。In Javascript, you can refer to an object properties using the square brackets
[]
just like what we do in an array.In this code, firstly we check whether the key is already defined in the object. If it is not defined, we will simply assign the value of
total_due
to the key (which in this case is the name of the person). In the next iteration, if Javascript recognises the key and the value of the key is not undefined, we simply add thetotal_due
value to the current key value.