如何遍历 javascript 对象并在新对象中求和
How to loop through a javascript object and sum amounts in a new object
我有一个 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>)....
如有任何帮助或提示,我们将不胜感激。
你 运行 出错了,因为 newObject.item
是 undefined
因为空对象因此你试图访问的任何 属性 都超出了 properties
未找到
通过对您的实施进行一些调整。我将名称添加到 newObject
如果它已经不存在然后我继续添加总数
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)
类似地,您可以使用 reduce
来解决这些类型的问题
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)
在Javascript中,您可以使用方括号[]
引用对象属性,就像我们在数组中所做的那样。
在这段代码中,首先我们检查对象中是否已经定义了键。如果未定义,我们将简单地将 total_due
的值分配给键(在本例中为人名)。在下一次迭代中,如果 Javascript 识别到键并且键的值不是未定义的,我们只需将 total_due
值添加到当前键值。
let finalData = {}
data.features.forEach((item) => {
if (finalData[item.properties.name] === undefined) {
finalData[item.properties.name] = item.properties.total_due;
}
else {
finalData[item.properties.name] += item.properties.total_due;
}
})
我有一个 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>)....
如有任何帮助或提示,我们将不胜感激。
你 运行 出错了,因为 newObject.item
是 undefined
因为空对象因此你试图访问的任何 属性 都超出了 properties
未找到
通过对您的实施进行一些调整。我将名称添加到 newObject
如果它已经不存在然后我继续添加总数
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)
类似地,您可以使用 reduce
来解决这些类型的问题
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)
在Javascript中,您可以使用方括号[]
引用对象属性,就像我们在数组中所做的那样。
在这段代码中,首先我们检查对象中是否已经定义了键。如果未定义,我们将简单地将 total_due
的值分配给键(在本例中为人名)。在下一次迭代中,如果 Javascript 识别到键并且键的值不是未定义的,我们只需将 total_due
值添加到当前键值。
let finalData = {}
data.features.forEach((item) => {
if (finalData[item.properties.name] === undefined) {
finalData[item.properties.name] = item.properties.total_due;
}
else {
finalData[item.properties.name] += item.properties.total_due;
}
})