Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

114
Views
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 answers
Answer question

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 Report

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!