Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

204
Views
extract data with same value from a grouped array

How can I group items with same nick value belonging from the same date into a new array(by nick and date) . Am trying the below but it produces the same array am trying to get from.

I want to group data by same user from the same date

var games = {
  '2021-12-15': [{
    nick: "john",
    gameDate: "2015-12-15 12:00:00",
    gameName: "bla 1"
  }, {
    nick: "john",
    gameDate: "2015-12-15 12:40:00",
    gameName: "bla 2"
  }],
  '2021-12-16': [{
      nick: "nicky",
      gameDate: "2016-12-16 12:00:00",
      gameName: "bla 3"
    }, {
      nick: "mike",
      gameDate: "2016-12-16 12:00:00",
      gameName: "bla 3"
    },
    {
      nick: "mike",
      gameDate: "2016-12-16 12:00:00",
      gameName: "bla 3"
    }

  ]
}
var a = {};
for (let val in games) {
  console.log(val)
  for (var i = 0; i < games[val].length; i++) {
    var d = games[val][i].gameDate.split(' ')[0];
    if (!a[d]) a[d] = [];
    a[d].push(games[val][i])
  }
}
console.log(a)


Expected output

{
  '2021-12-15': [{
    nick: "john" {
      nick: "john",
      gameDate: "2016-12-15 12:00:00",
      gameName: "bla 3"
    },
    {
      nick: "john",
      gameDate: "2016-12-15 12:30:00",
      gameName: "bla 3"
    }
  }],
  '2021-12-16': [{
    nick: "mike" {
      nick: "mike",
      gameDate: "2016-12-16 12:00:00",
      gameName: "bla 3"
    },
    {
      nick: "mike",
      gameDate: "2016-12-16 12:15:00",
      gameName: "bla 3"
    }
  }]
}

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

Hard to tell without an exact output format but I think this is what you're after:

const games = {
  "2021-12-15": [
    {
      nick: "john",
      gameDate: "2015-12-15 12:00:00",
      gameName: "bla 1",
    },
    {
      nick: "john",
      gameDate: "2015-12-15 12:40:00",
      gameName: "bla 2",
    },
  ],
  "2021-12-16": [
    {
      nick: "nicky",
      gameDate: "2016-12-16 12:00:00",
      gameName: "bla 3",
    },
    {
      nick: "mike",
      gameDate: "2016-12-16 12:00:00",
      gameName: "bla 3",
    },
    {
      nick: "mike",
      gameDate: "2016-12-16 12:00:00",
      gameName: "bla 3",
    },
  ],
};

const result = Object.fromEntries(
  Object.entries(games).map(([key, value]) => [
    key,
    value.reduce(
      (acc, cur) => (acc[cur.nick] = [...(acc[cur.nick] || []), cur], acc),
      {}
    ),
  ])
);

console.log(result);

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!