Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

90
Visualizações
I want to separate my array into other arrays by matchday

I'm trying to separate a huge array in order of matchday. There are 38 matchdays in total, I'm trying to display the games by matchdays.

I have this

 data.matches = [{matchday: 1, team: xxx}, {matchday: 1, team: xxx}, {matchday: 2, team: xxx} etc..]

I would like something like this

  data.matches = [[{matchday: 1, team: xxx}, {matchday: 1, team: xxx} ],[{matchday: 2, team: xxx}] etc..]

So you can see I create a new array for each different matchday, those new arrays will be nested inside the main array.

My poor attempt:

let results: any = [];

    if (isSuccess) {
        data.matches.map((item: any) => {
            for (let i = 1; i < 38; i++) {
                if (item.matchday === i) {
                    results.push(item);
                } else {
                    results.splice(i, 0, item);
                }
            }
        });
        console.log(results);

    }
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You can use a reducer for that. Reduce to object with matchdays keys, and retrieve its values. Something like:

const matches = [
  {matchday: 1, team: `yxx`}, 
  {matchday: 1, team: `xyx`}, 
  {matchday: 2, team: `xxy`},
  {matchday: 15, team: `yyx`},
  {matchday: 15, team: `yyy`} ];
const byDay = Object.values(
  matches.reduce( (acc, res) => {
    acc[`day${res.matchday}`] = acc[`day${res.matchday}`] 
      ? acc[`day${res.matchday}`].concat(res) : [res];
    return acc;}, {} )
  );
console.log(byDay);
.as-console-wrapper {
    max-height: 100% !important;
}

about 4 years ago · Juan Pablo Isaza Relatório

0

You can use this method:

const matches = [{matchday: 1, team: "xxx"}, {matchday: 1, team: "xxx"}, {matchday: 2, team: "xxx"}]
const result = Array.from(matches
        .reduce((m, val) => m.set(val.matchday, [...(m.get(val.matchday) || []), val]), new Map)
        .values()
);
console.log(result)

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda