Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count wins, games and type of game in array of objects and create object of objects

I am sure there is already an answer as well as an "easy" solution to this, but I did not find something in the last hour.

So I have this object:

[
  {
    "win": false,
    "switched": false
  },
  {
    "win": false,
    "switched": false
  },
  {
    "win": true,
    "switched": true
  },
  {
    "win": true,
    "switched": true
  }
]

which is showing the results of four monty-hall games. I want to create an object from this array that would have the following structure:

{
    switched: {
        played: 2,
        won: 2
    },
    put: {
        played: 2,
        lost: 2
    }
}

So actually if the object in the upper array has switched: true I want to increase the value of switched["played"]by 1 and it has also won: true I want to increase the value of the keys switched["won"] also by one. Vice versa for switched: false I wand to put everything in the results-object under the key put.

I tried some rather embarrassing approaches with reduce, but I think there must be an "easier" way (mine did not work at all...)

like image 747
Lenn Avatar asked Dec 14 '25 22:12

Lenn


2 Answers

const items = [{
    win: false,
    switched: false
  },
  {
    win: false,
    switched: false
  },
  {
    win: true,
    switched: true
  },
  {
    win: true,
    switched: true
  }
];

const res = items.reduce(
  (acc, cur) => {
    // If your current item is switched
    return cur["switched"]
      ? { // ... deal with the switched part, leave the rest as is
          ...acc,
          switched: {
            played: acc.switched.played + 1, // increase the played counter by 1, based on whatever your accumulator currently has
            won: acc.switched.won + Number(cur.win) // cast your win property to number (true -> 1, false -> 0) and increase accordngily
          }
        }
      : { // ... otherwise deal with the put part, leave the rest as is
          ...acc,
          put: {
            played: acc.put.played + 1,
            lost: acc.put.lost + Number(!cur.win) // negate your win so that it reflects a loss, cast to number and increase accordingly
          }
        };
  },
  // Initialize your accumulator
  {
    switched: {
      played: 0,
      won: 0
    },
    put: {
      played: 0,
      lost: 0
    }
  }
);

console.log(res);
like image 113
antoniom Avatar answered Dec 16 '25 14:12

antoniom


I think it might help you

const data = [{"win": false,"switched": false},{"win": false,"switched": false},{"win": true,"switched": true},{"win": true,"switched": true}];

const initScores = {
    switched: {
        played: 0,
        won: 0,
    },
    put: {
        played: 0,
        lost: 0,
    }
};

const result = data.reduce((acc, {win, switched})=> {
    if (switched) {
        acc.switched.played += 1;
        acc.switched.won += win ? 1 : 0;
    } else {
        acc.put.played += 1;
        acc.put.lost += !win ? 1 : 0;
    }
    return acc;
}, initScores);

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0 }
like image 22
Alexandr Belan Avatar answered Dec 16 '25 14:12

Alexandr Belan



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!