【发布时间】: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].score是null。 -
this.seedmatches[i]不是空值,它是其中之一上的score对象。 -
它就在您发布的数组中:最后几个条目有
"score": null。 -
@JeremyHarris 我想在 seedmatches[i] 为 NULL 时跳过该值。 Butt 不知何故它不会跳过它,这就是为什么我得到 setOne 的错误,因为它是 null
-
@SajidLatif 您没有阅读 cmets 或答案吗?问题是某些对象的 score 属性是
null。 查看您自己在问题中发布的数组。
标签: javascript arrays vue.js object