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

134
Vistas
How does one, from a list of objects, group and collect just specific values which are explicitly referred to, each by its key?

I have a data structure like

const dd = [{
  keyone: "test",
  two: "you",
  three: 'op',
}, {
  keyone: "youuuu",
  two: "ttt",
  three: 'op',
}, {
  keyone: "operation",
  two: "test",
  three: 'op',
}];

And I am wanting to be able to pull out keyone and two into an object like the following

const obj = { keyone: ['test', 'youuuu', 'operation'], two: ['you', 'ttt', 'test']}

I've achieved this using two maps and combining them but I would like to use only one loop if possible.

EDIT:

I am currently using destructuring to extract values:

const mapOne = dd.map(({ keyone }) => keyone);
const mapTwo = dd.map(({ two }) => two);
const test = {
  keyone: mapOne,
  two: mapTwo,
};
about 4 years ago · Juan Pablo Isaza
3 Respuestas
Responde la pregunta

0

You could take an array of keys and group the values together.

const
    data = [{ keyone: "test", two: "you", three: 'op' }, { keyone: "youuuu", two: "ttt", three: 'op' }, { keyone: "operation", two: "test", three: 'op' }],
    keys = ['keyone', 'two'],
    grouped = data.reduce((r, o) => {
        keys.forEach(k => (r[k] ??= []).push(o[k]));
        return r;
    }, {});

console.log(grouped);

about 4 years ago · Juan Pablo Isaza Denunciar

0

A generically implemented, thus re-usable and configurable function, which does exactly what the OP did ask for, would be close to the next provided example code ...

function groupAndCollectSpecifcEntriesOnly(collector, item) {
  const { keyList, result } = collector;

  keyList.forEach(key => {
    if (item.hasOwnProperty(key)) {

      (result[key] ??= []).push(item[key])
    }
  });
  return collector;
}

const sampleData = [{
  keyone: "test",
  two: "you",
  three: 'op',
}, {
  keyone: "youuuu",
  two: "ttt",
  three: 'op',
}, {
  keyone: "operation",
  two: "test",
  three: 'op',
}];

console.log(
  sampleData.reduce(groupAndCollectSpecifcEntriesOnly, {

    keyList: ['keyone', 'two'],
    result: {},

  }).result
);
console.log(
  sampleData.reduce(groupAndCollectSpecifcEntriesOnly, {

    keyList: ['three', 'keyone'],
    result: {},

  }).result
);
.as-console-wrapper { min-height: 100%!important; top: 0; }

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do something like this:

const dd = [
{
  keyone: "test",
  two: "you",
  three: 'op'
},
{
  keyone: "youuuu",
  two: "ttt",
  three: 'op'
},
{
  keyone: "operation",
  two: "test",
  three: 'op'
}];
const result = dd.reduce((prev, acc, arr ) => {
       if (acc['keyone']) { prev['keyone'].push(acc['keyone']) } 
       if (acc['two']) { prev['two'].push(acc['two']) }
       return prev;
}, {keyone: [], two: []});

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