Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

124
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda