-2

I have a very very basic operation which I just can't do for some weird reason. I have an array of games:

[

{gameName: "Dota 2", gamePlayTime: 140}

{gameName: "Dota 2", gamePlayTime: 100}

{gameName: "Dota 2", gamePlayTime: 176}

{gameName: "Dota 2", gamePlayTime: 176}

{gameName: "Counter-Strike", gamePlayTime: 0}

{gameName: "War Thunder", gamePlayTime: 90}

{gameName: "Dota 2", gamePlayTime: 156}

{gameName: "Dota 2", gamePlayTime: 105}

{gameName: "Path of Exile", gamePlayTime: 78}

{gameName: "Counter-Strike: Source", gamePlayTime: 116}

{gameName: "Counter-Strike: Global Offensive", gamePlayTime: 75}

{gameName: "Dota 2", gamePlayTime: 67}

]

What I would like to do is just simply count how many hours the game is played like this:

[

{gameName: "Dota 2", gamePlaytime: TOTAL AMOUNT}

{gameName: "War Thunder", gamePlaytime: TOTAL AMOUNT}

{gameName: "Counter Strike", gamePlaytime: TOTAL AMOUNT}

]

So drop the duplicate values and count the total playtime per game. I have tried to use reduce but cant understand this.

EDIT: I was looking for elegant ES6 solution to do this, the duplicate answer doesn't provide "clean" solution in my opinion. It is a similar question though. I just didn't know the proper search words.

Shnigi
  • 972
  • 9
  • 19
  • 1
    Hi! Please take the [tour] (you get a badge!) and read through the [help], in particular [*How do I ask a good question?*](/help/how-to-ask) Your best bet here is to do your research, [search](/help/searching) for related topics on SO, and give it a go. ***If*** you get stuck and can't get unstuck after doing more research and searching, post a [mcve] of your attempt and say specifically where you're stuck. People will be glad to help. – T.J. Crowder Nov 21 '19 at 08:57
  • Thanks @Andreas. I couldn't find it, and I knew it had to be there! :-) – T.J. Crowder Nov 21 '19 at 09:01
  • 1
    I really cant get my head over with this community. Asking once a year and getting so much down votes for simple question. I have used search, couldn't find relevant article. – Shnigi Nov 21 '19 at 09:08
  • If it helps, I don't understand the downvotes either. Duplicates should get close votes and, if appropriate, a comment helping with understanding how the dupetarget works in this case (not answers). Downvoting should be reserved for *bad* questions or *really, really obvious* duplicates where the original is *easily* found. Not the case here IMHO. – T.J. Crowder Nov 21 '19 at 09:15
  • I totally agree with you guys. Too many downvotes when they are absolutely not relevant. Too negative community. – Dmytro Huz Nov 21 '19 at 09:20
  • On your edit: There's plenty of "ES6" stuff in the answers on the dupe target. – Andreas Nov 21 '19 at 09:53

2 Answers2

0

You can use .reduce() for that:

const input = [
  {gameName: "Dota 2", gamePlayTime: 140},
  {gameName: "Dota 2", gamePlayTime: 100},
  {gameName: "Dota 2", gamePlayTime: 176},
  {gameName: "Dota 2", gamePlayTime: 176},
  {gameName: "Counter-Strike", gamePlayTime: 0},
  {gameName: "War Thunder", gamePlayTime: 90},
  {gameName: "Dota 2", gamePlayTime: 156},
  {gameName: "Dota 2", gamePlayTime: 105},
  {gameName: "Path of Exile", gamePlayTime: 78},
  {gameName: "Counter-Strike: Source", gamePlayTime: 116},
  {gameName: "Counter-Strike: Global Offensive", gamePlayTime: 75},
  {gameName: "Dota 2", gamePlayTime: 67}
]

const result = input.reduce((res, curr) => {

    const existing = res.find((e) => e.gameName === curr.gameName);
    if (existing) {
        existing.gamePlayTime += curr.gamePlayTime;
    } else {
        res.push(curr);
    }

    return res;
}, []);

console.log(result);
Sebastian Kaczmarek
  • 8,120
  • 4
  • 20
  • 38
  • You *can*, but there's no good reason to. All `reduce` does here is add complexity. *(Edit: **Not** my downvote)* – T.J. Crowder Nov 21 '19 at 08:59
  • What do you mean by *add complexity*? It seems like a `groupBy` + `sum` operation. Isn't `reduce()` the simplest and fastest way to achieve that? – Sebastian Kaczmarek Nov 21 '19 at 09:02
  • When you're using `reduce` but not changing the accumulator (you're returning the same array every time), a simple loop is simpler and easier to understand. – T.J. Crowder Nov 21 '19 at 09:04
  • While you may be right, I still don't understand the downvote. The code works and produces the output as expected. It's still pretty simple to understand and fast enough. – Sebastian Kaczmarek Nov 21 '19 at 10:29
  • I wouldn't downvote this answer. Some people do downvote answers to duplicates regardless of whether the answer is correct, it could have been one of those people. – T.J. Crowder Nov 21 '19 at 10:42
  • Ah, ok, I thought it was you. Thanks for the suggestions anyway – Sebastian Kaczmarek Nov 21 '19 at 10:46
  • 1
    Yeah, the person commenting and the person downvoting are rarely the same person if they don't say they downvoted. :-) Happy coding! – T.J. Crowder Nov 21 '19 at 10:47
0
//create an variable with games data
let games = [
{gameName: "Dota 2", gamePlayTime: 140},
{gameName: "Dota 2", gamePlayTime: 100},
{gameName: "Dota 2", gamePlayTime: 176},
{gameName: "Dota 2", gamePlayTime: 176},
{gameName: "Counter-Strike", gamePlayTime: 0},
{gameName: "War Thunder", gamePlayTime: 90},
{gameName: "Dota 2", gamePlayTime: 156},
{gameName: "Dota 2", gamePlayTime: 105},
{gameName: "Path of Exile", gamePlayTime: 78},
{gameName: "Counter-Strike: Source", gamePlayTime: 116},
{gameName: "Counter-Strike: Global Offensive", gamePlayTime: 75},
{gameName: "Dota 2", gamePlayTime: 67}
]

let result = {}
//iterate each game in array and check its name. 
//If it is already in result object - add play time to that, 
// if it is not - add this game name to result object and set play time
for(let game of games){
    if (game["gameName"] in result){
        result[game["gameName"]] += game["gamePlayTime"]
    } else {
        result[game["gameName"]] = game["gamePlayTime"]
    }
}
// result - is an object {gameName:"", gameTotalPlayTime:""}
console.log(result)
Dmytro Huz
  • 1,514
  • 2
  • 14
  • 22
  • For me this seems okay. Thanks! There might not be explanation what you did which is why somebody down voted but I can see what is happening here so this works for me. – Shnigi Nov 21 '19 at 09:12
  • thanks, I added an explanation in case it can be helpful for other users. – Dmytro Huz Nov 21 '19 at 09:16