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

109
Vistas
Reduce an array to an 2D array with matching properties

If I have one array of objects with inner properties like so:

var array = [
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.001.003",
    },
    {
        "Structure_SortID": "001.004.002",
    },
    {
        "Structure_SortID": "001.004.002",
    }
];

How can I reduce this one array into a single 2D array that contains 2 arrays of matching "Structure_SortID" properties? Output should be:

var array = [
    // Filtered so only 001.004.002
    [
        {
            "Structure_SortID": "001.001.003",
        },
        {
            "Structure_SortID": "001.001.003",
        },
        {
            "Structure_SortID": "001.001.003",
        },
        {
            "Structure_SortID": "001.001.003",
        }
    ],
    // Filtered so only 001.004.002
    [
        {
            "Structure_SortID": "001.004.002",
        },
        {
            "Structure_SortID": "001.004.002",
        }
    ]
];

The solution needs to allow for an infinite number of lists with matching "Structre_SortID" properties. And needs to support situations where there is only 1 array of matching ID's inside the 2D array. And needs to support empty arrays or single item arrays alongside matching lists inside the 2D array.

about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

function getArrayOfObjectByKey(data, key) {
    return Object.values(
        data.reduce((acc, e) => {
            if (key in e) {
                let value = e[key];
                if (value in acc) {
                    acc[value].push(e);
                } else {
                    acc[value] = [e];
                }
            }
            return acc;
        }, {})
    );
}
getArrayOfObjectByKey(array, 'Structure_SortID');
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