Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

88
Vistas
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 Respuestas
Responde la pregunta

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 Denunciar

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 Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda