【问题标题】:Object value loop对象值循环
【发布时间】:2020-06-05 23:06:22
【问题描述】:

我正在尝试使用 if 语句进行循环,其中 if 语句应该跳过空值。但有些如何它不会识别空值。

我的数组对象值有这个

[ { "id": 17, "match_id": 17, "dktournament_id": 10, "seed_match": 1, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:53", "score": { "id": 17, "playerOne": "Knud", "playerTwo": "Weise", "setOne": 3, "setTwo": 0, "created_at": "2020-02-11 21:37:53", "updated_at": "2020-02-11 21:38:21" } }, { "id": 18, "match_id": 18, "dktournament_id": 10, "seed_match": 2, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:39:07", "score": { "id": 18, "playerOne": "Hans", "playerTwo": "Khan", "setOne": 0, "setTwo": 3, "created_at": "2020-02-11 21:39:07", "updated_at": "2020-02-11 21:39:42" } }, { "id": 19, "match_id": 19, "dktournament_id": 10, "seed_match": 3, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:44:12", "score": { "id": 19, "playerOne": "Preben", "playerTwo": "Gertrud", "setOne": 1, "setTwo": 0, "created_at": "2020-02-11 21:44:12", "updated_at": "2020-02-21 12:24:39" } }, { "id": 20, "match_id": 20, "dktournament_id": 10, "seed_match": 4, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:58:02", "score": { "id": 20, "playerOne": "Ingvard", "playerTwo": "Oscar", "setOne": 0, "setTwo": 0, "created_at": "2020-02-11 21:58:02", "updated_at": "2020-02-11 21:58:02" } }, { "id": 21, "match_id": null, "dktournament_id": 10, "seed_match": 5, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 22, "match_id": null, "dktournament_id": 10, "seed_match": 6, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 23, "match_id": null, "dktournament_id": 10, "seed_match": 7, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 24, "match_id": null, "dktournament_id": 10, "seed_match": 8, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null } ]

我的方法是这样的

totalsetsTeamOne(){
                let i;
                let SetOneTotal = 0;
                for (i = 0; i < 8; i++) {
                    if(this.seedmatches[i]!= null){
                        SetOneTotal += this.seedmatches[i].score.setOne;
                    }
                }
                this.setOneTotal = SetOneTotal;
                console.log(SetOneTotal)
            },

我的错误是这样的

[Vue warn]: Error in mounted hook: "TypeError: Cannot read property 'setOne' of null"

【问题讨论】:

  • 错误表示this.seedmatches[i].scorenull
  • this.seedmatches[i] 不是空值,它是其中之一上的score 对象。
  • 它就在您发布的数组中:最后几个条目有"score": null
  • @JeremyHarris 我想在 seedmatches[i] 为 NULL 时跳过该值。 Butt 不知何故它不会跳过它,这就是为什么我得到 setOne 的错误,因为它是 null
  • @SajidLatif 您没有阅读 cmets 或答案吗?问题是某些对象的 score 属性是null查看您自己在问题中发布的数组。

标签: javascript arrays vue.js object


【解决方案1】:

这里有两个版本的计算,使用表达式。

第二个使用optional chaining operator

const data = [ { "id": 17, "match_id": 17, "dktournament_id": 10, "seed_match": 1, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:53", "score": { "id": 17, "playerOne": "Knud", "playerTwo": "Weise", "setOne": 3, "setTwo": 0, "created_at": "2020-02-11 21:37:53", "updated_at": "2020-02-11 21:38:21" } }, { "id": 18, "match_id": 18, "dktournament_id": 10, "seed_match": 2, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:39:07", "score": { "id": 18, "playerOne": "Hans", "playerTwo": "Khan", "setOne": 0, "setTwo": 3, "created_at": "2020-02-11 21:39:07", "updated_at": "2020-02-11 21:39:42" } }, { "id": 19, "match_id": 19, "dktournament_id": 10, "seed_match": 3, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:44:12", "score": { "id": 19, "playerOne": "Preben", "playerTwo": "Gertrud", "setOne": 1, "setTwo": 0, "created_at": "2020-02-11 21:44:12", "updated_at": "2020-02-21 12:24:39" } }, { "id": 20, "match_id": 20, "dktournament_id": 10, "seed_match": 4, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:58:02", "score": { "id": 20, "playerOne": "Ingvard", "playerTwo": "Oscar", "setOne": 0, "setTwo": 0, "created_at": "2020-02-11 21:58:02", "updated_at": "2020-02-11 21:58:02" } }, { "id": 21, "match_id": null, "dktournament_id": 10, "seed_match": 5, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 22, "match_id": null, "dktournament_id": 10, "seed_match": 6, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 23, "match_id": null, "dktournament_id": 10, "seed_match": 7, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null }, { "id": 24, "match_id": null, "dktournament_id": 10, "seed_match": 8, "created_at": "2020-02-11 21:37:47", "updated_at": "2020-02-11 21:37:47", "score": null } ]

const setOneTotal1 = (arr) => 
    arr.reduce((total, { score }) =>
        ((score && (total += score.setOne)), total), 0)
console.log(setOneTotal1(data))

const setOneTotal2 = (arr) => 
    arr.reduce((total, {score}, _, __, s1 = score?.setOne) => 
        (s1 ? total + s1 : total), 0)
console.log(setOneTotal2(data))

【讨论】:

    【解决方案2】:

    此代码将起作用..试试这个...

    totalsetsTeamOne() {
        let i;
        let SetOneTotal = 0;
        for (i = 0; i < 8; i++) {
            if (this.seedmatches[i] && this.seedmatches[i].score !== null) {
                SetOneTotal += seedmatches[i].score.setOne;
            }
        }
        this.setOneTotal = SetOneTotal;
        console.log(SetOneTotal)
    },
    

    【讨论】:

      【解决方案3】:

      你的 if 语句应该是

       if(this.seedmatches[i] && this.seedmatches[i].score){
            SetOneTotal += this.seedmatches[i].score.setOne;
       }
      

      因为您只检查 this.seedmatches[i] 中是否有值。但是如果 this.seedmatches[i] 中有值但 this.seedmatches[i].score 为空怎么办?因此,我们需要彻底检查 this.seedmatches[i].score 是否也为空。

      【讨论】:

        【解决方案4】:

        您正在查看 this.seedmatches[i] 是否为空,而不是 this.seedmatches[i].score 是否为空

        【讨论】:

          【解决方案5】:

          因为 "id": 21 和其他项目有 "score":null

          【讨论】:

          • 我知道。这就是为什么我想跳过它
          猜你喜欢
          • 2015-10-14
          • 1970-01-01
          • 2016-02-21
          • 1970-01-01
          • 1970-01-01
          • 2012-03-12
          • 2013-07-14
          相关资源
          最近更新 更多