如何循环遍历 JavaScript 对象并对新对象中的金额求和

发布于 2025-01-10 12:51:41 字数 1647 浏览 1 评论 0原文

我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

孤檠 2025-01-17 12:51:41

您遇到了错误,因为 newObject.item未定义,因为空对象因此您尝试访问的任何属性(例如 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)

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)

说不完的你爱 2025-01-17 12:51:41

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

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 the total_due value to the current key value.

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;
    }
})
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文