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

122
Views
How to dynamically change the key of the objects and merge multiple array like objects into single array using java script?

I have done the pre-processing calculation using javascript and finally have the following different arrays.

var medianArray = [{ Thomas: 94, Diana: 94, Claura: 93, Chandler: 91 }];
var q1Array = [ { Thomas: 93, Diana: 94, Claura: 92, Chandler: 91 } ];
var q3Array = [{ Thomas: 95, Diana: 95, Claura: 93, Chandler: 93 }];
var outArray = [{ Thomas: [], Diana: [85, 86, 87, 91], Claura: [94, 94, 94], Chandler: [75, 86] }];
var result = [];

now, I am trying to bring into a single array with meaningful keys and values in the below format.

[{"stdName": "Thomas", "q1" : 93, "q3" : 95, "median": 94, "outliers": []},
{"stdName": "Diana", "q1" : 93, "q3" : 95, "median": 94, "outliers": [85, 86, 87, 91]},
{"stdName": "Claura", "q1" : 93, "q3" : 95, "median": 93, "outliers": [94, 94, 94]},
{"stdName": "Chandler", "q1" : 93, "q3" : 95, "median": 91, "outliers": [75, 86]}]

I have searched tried multiple online resources and references of reduce(), map(), etc., but somehow not getting how to change the key name solutions.

[].concat(q1Array, q3Array, medianArray,outArray).forEach(function (items) {
    //logic try to add and not getting idea to proceed.
    if (!this[items]) {            
        result.push();
    }   

}, Object.create(null));

console.log(result);

I hope you kind of JavaScript people on Stack Overflow can help me out with this.

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

If all arrays have the same amount of names you can first extract all names with Object.keys(). Afterwards loop over all the names and reduce them into their own object.

var medianArray = [{ Thomas: 94, Diana: 94, Claura: 93, Chandler: 91 }];
var q1Array = [ { Thomas: 93, Diana: 94, Claura: 92, Chandler: 91 } ];
var q3Array = [{ Thomas: 95, Diana: 95, Claura: 93, Chandler: 93 }];
var outArray = [{ Thomas: [], Diana: [85, 86, 87, 91], Claura: [94, 94, 94], Chandler: [75, 86] }];

const names = Object.keys(medianArray[0]);

const result = names.reduce((acc, name) => {
  acc.push({
    stdName: name,
    q1: q1Array[0][name],
    q3: q3Array[0][name],
    median: medianArray[0][name],
    outliers: outArray[0][name],
  });
  
  return acc;
}, []);

console.log(result);

about 4 years ago · Juan Pablo Isaza 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!