简体   繁体   English

从数组中提取对象时减少返回 NaN 的函数 - MongoDB 数据库,React 前端

[英]Reduce function returning NaN when pulling object from array - MongoDB database, React frontend

I have the following Schema:我有以下架构:

const SubmitDebtSchema = new Schema ({
  balance: [{
    balanceDate: Date,
    newBalance: Number
  }],
});

I am attempting to loop through my database entries, pull the 'newBalance' out from each object in the balance array, and then reduce / sum them together.我试图遍历我的数据库条目,从 balance 数组中的每个对象中取出“newBalance”,然后将它们减少/相加。

However, it is returning 'NaN' - and I can't figure out why.但是,它正在返回 'NaN' - 我不知道为什么。

Here is my Axios call to get the data:这是我获取数据的 Axios 调用:

  componentDidMount() {

    axios.get("/api/fetch/fetchDebtCards")
    .then((response) => {
      this.setState({
        debts: response.data
      })
      console.log(this.state.debts.balance.newBalance)
    })
  }

The console log in there successfully retrieves the database entries.那里的控制台登录成功检索了数据库条目。

And here is my reduce function:这是我的减少功能:

const sumBalance = this.state.debts.reduce(function(previousValue, currentValue) {
          return (
            previousValue + currentValue.balance.newBalance
          )
        }, 0)

You can see, I'm attempting to tap into 'balance.newBalance' to access the newBalance within each of the balance objects.您可以看到,我正在尝试利用“balance.newBalance”来访问每个 balance 对象中的 newBalance。

Can anyone point out what I'm doing wrong?谁能指出我做错了什么?

EDIT: My console log, with two entries.编辑:我的控制台日志,有两个条目。 What I want to do is get the newBalance array.length -1 from these, and sum them together by reducing.我想要做的是从这些中获取 newBalance array.length -1,并通过减少将它们相加。

[Log] Array [日志] 数组

0 Object 0 对象

balance: [{_id: "5fbbddd1077c56000828973c", balanceDate: "2020-11-23T16:05:36.124Z", newBalance: 400}]余额:[{_id:“5fbbddd1077c56000828973c”,余额日期:“2020-11-23T16:05:36.124Z”,新余额:400}]

1 Object 1 个对象

balance: [{_id: "5fbc06f58b2f98000865df54", balanceDate: "2020-11-23T19:01:07.789Z", newBalance: 300}] (1)余额:[{_id:"5fbc06f58b2f98000865df54", balanceDate: "2020-11-23T19:01:07.789Z", newBalance: 300}] (1)

if "console.log(this.state.debts.balance.newBalance)" works then debts is not an array, how are you using map on debts then?如果“console.log(this.state.debts.balance.newBalance)”有效,则债务不是数组,那么您如何在债务上使用地图? Map can only be used on arrays. Map 只能用于数组。

I'm not sure how your debts object/array is exactly.我不确定你的债务对象/数组到底是怎样的。 Maybe a log of it would be helpful.也许它的日志会有所帮助。

If it is an array, then this might work.如果它是一个数组,那么这可能有效。

const sumBalance = this.state.debts.map(x => x.balance).flat().reduce((a,b) => a + b.newBalance, 0)

whereas if it's an object, then this might work而如果它是一个对象,那么这可能会起作用

const sumBalance = this.state.debts.balance.reduce((a,b) => a+b.newBalance, 0)

If none of these work, just log "this.state.debts" and let us see what you have there.如果这些都不起作用,只需记录“this.state.debts”,让我们看看你有什么。

Edit: Ok so you only need the last values of the balance arrays (the latest balance), something like this?编辑:好的,所以你只需要余额数组的最后一个值(最新的余额),像这样?

const sumBalance = this.state.debts.map(x => x.balance[x.balance.length-1].newBalance).reduce((a,b) => a + b, 0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM