[Vuejs]-Object value loop

4👍

Your if statement should be

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

Because you are only checking if there are values in this.seedmatches[i]. But what if there are values in this.seedmatches[i] but this.seedmatches[i].score is null? Therefore, we need to check thoroughly if this.seedmatches[i].score is null also.

3👍

Because “id”: 21 and other items have “score”:null

2👍

You are looking if this.seedmatches[i] but not if this.seedmatches[i].score is null

0👍

This code will work..Try this…

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)
},

0👍

Here are two versions of the calculation, using expressions.

The second uses the 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))

Leave a comment