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

168
Vistas
How to merge objects and append it into new array if object values are same using JavaScript?

I have this kind of data, my goal is to find all same request_id values in the given objects, merge it to one object and append its unique_id into one array, How can I get this result?

let fakeData = {
    "622b1e4a8c73d4742a66434e212":{
      request_id: "1",
      uique_id:'001'
    },
    "622b1e4a8c73d4742a6643423e54":{
        request_id: "1",
        uique_id:'002'
    },
    "622b1e4a8c73d4742a6643423e23":{
        request_id: "2",
        uique_id:'003'
    },
  }

  let parts = []

  for (const property in fakeData) {
      console.log(fakeData[property]);
  }


  //Result should be
  // [{request_id:'1', values:["001, 002"]}, {request_id:'2', values:["003"]}]

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

0

You can iterate over all the values using Object.values() and array#reduce. In the array#reduce group based on request_id in an object and extract all the values of these object using Object.values().

const fakeData = { "622b1e4a8c73d4742a66434e212": { request_id: "1", uique_id: '001' }, "622b1e4a8c73d4742a6643423e54": { request_id: "1", uique_id: '002' }, "622b1e4a8c73d4742a6643423e23": { request_id: "2", uique_id: '003' }, },
      output = Object.values(fakeData).reduce((r, {request_id, uique_id}) => {
        r[request_id] ??= {request_id, values: []};
        r[request_id].values.push(uique_id);
        return r;
      },{}),
      result = Object.values(output);
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

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