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

124
Vistas
Add key and value to an array using javascript

I have two objects and I need to create one new array with a new structure for example: These are my objects:

Object 1: [{ "Question":"Climate change", "Total":"3.456" }]

Object 2: [{ "Question":"Climate change", "Total":"3.567" }]

I need this Output

[{ "x":3.456, "y":3.567 }]

I tried creating the array I need with one of the arrays... So I used the first array to have: [{y:3.456}] and it works, now I need to add the x key and the value from the second array so I tried this:

Where chardatavalue is one of the arrays(which contains the "y" key and the value) and arrayforxls2 is the other one, but the code I have only assign the last value of the seconf array to all of the "x" keys.

var resultnew = chartdatavalue.map(function (el) {
 var o = Object.assign({}, el);

     for (let i of arrayforxls2) {
         
         o.x = i.Total;
                
      }
      return o;
    });

chartdatavalue has this structure:

[{
y: '3.893'
}]

arrayxls2 has this structure:

[{
"Question":"Climate change",
"Total":"3.567"
}]

I need this output:

[{ "x":3.567, "y":3.893 }] Can you please help me.

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

0

Possibly you are looking for this:

var resultnew = chartdatavalue.map(function (el, i) {
    return Object.assign({ x: arrayforxls2[i].Total }, el);
});

The change here is that i is captured and used, so that the ith object from the one array is merged with the ith of the other.

You can also use object spread syntax instead of Object.assign for this:

var resultnew = chartdatavalue.map((el, i) =>
    ({ x: arrayforxls2[i].Total, ...el})
);

Or, when there are other properties in el that you need to ignore, then with object destructuring:

var resultnew = chartdatavalue.map(({y}, i) =>
    ({ x: arrayforxls2[i].Total, y})
);
about 4 years ago · Juan Pablo Isaza Denunciar

0

Something like this:

var resultnew = chartdatavalue.map((el, i) => ({x: el.Total, y: arrayforxls2[i].Total}));

This iterates over one of the arrays, and uses the index i to get the value from the corresponding object in the other array.

There's no point in using Object.assign() since the result objects don't include any of the property names of the input objects.

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