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

115
Vistas
JavaScript merge array objects with the same keys

I have an array with several objects some have the same keys (Question and QuestionId), for example:

    var jsonData = [
            {
                Question: "Was the training useful?",
                QuestionId: 1,
                myData: [{ name: 'No', value: 1 }] },
            {
                Question: "Was the training useful?",
                QuestionId: 1 ,
                myData: [{ name: 'Yes', value: 1 }]
        }];

How can I combine these objects into one, expected output:

      var jsonData = [
        {
            Question: "Was the training useful?",
            QuestionId: 1,
            myData: [{ name: 'No', value: 1 },
                     { name: 'Yes', value: 1 }] 
          }];
about 4 years ago · Santiago Gelvez
3 Respuestas
Responde la pregunta

0

var jsonData = [{
    Question: "Was the training useful?",
    QuestionId: 1,
    myData: [{
      name: 'No',
      value: 1
    }]
  },
  {
    Question: "Was the training useful?",
    QuestionId: 1,
    myData: [{
      name: 'Yes',
      value: 1
    }]
  }
];

const result = Object.values(jsonData.reduce((acc, obj) => {
  if (!acc[obj.QuestionId]) {
    acc[obj.QuestionId] = obj;
  } else {
    acc[obj.QuestionId].myData = acc[obj.QuestionId].myData ?? []; // if myData is null or undefined, set it to an empty array
    acc[obj.QuestionId].myData = acc[obj.QuestionId].myData.concat(obj.myData);
  }
  return acc;
}, {}));

console.log(result);

about 4 years ago · Santiago Gelvez Denunciar

0

function mergeData(data) {
  const acc = {}
  data.forEach(x => {
    const id = x.QuestionId
    if (!acc[id]) acc[id] = x
    else acc[id].myData = acc[id].myData.concat(x.myData)
  })
  return Object.values(acc)
}
about 4 years ago · Santiago Gelvez Denunciar

0

You can use forEach to create an object where the keys are the question IDs and then use Object.values to get the values into an array.

var jsonData = [{
  Question: "Was the training useful?",
  QuestionId: 1,
  myData: [{ name: 'No', value: 1 }] 
}, {
  Question: "Was the training useful?",
  QuestionId: 1 ,
  myData: [{ name: 'Yes', value: 1 }]
}];

var temp = {};
jsonData.forEach(x=>{
  if(!temp[x.QuestionId]) {temp[x.QuestionId] = x;} 
  else {temp[x.QuestionId].myData.push(x.myData);}
});

var arr = Object.values(temp);

console.log(arr);

about 4 years ago · Santiago Gelvez 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