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

71
Vistas
How to add text in key value pair Object

I had two array data which I was able to convert into a key value pair object.

firstArray: [] = ['a','b','c','d']
secondArray: [] = [1,2,3,4]

Code to convert two array to Key Value Pair:

let dict = firstArray.map(function(obj, index) {
let mydict = {}
mydict[secondArray[index]] = obj;
return mydict;
});

console.log(dict) 
Output: 
 [{
  "1": "a"
}, {
  "2": "b"
}, {
  "3": "c"
}, {
  "4": "d"
}] 

Desired Output:

[{
 value: "1", name: "a"
}, {
value:  "2", name: "b"
}, {
value:  "3",name: "c"
}, {
 value: "4", name:"d"
}]

Can someone help me figure out how to achieve this.

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

0

const firstArray = ['a','b','c','d'];
const secondArray = [1,2,3,4];

const dict = firstArray.map((i, index) => {
   return {
    value: secondArray[index].toString(),
    name: i
   };     
});
console.log(dict);

about 4 years ago · Juan Pablo Isaza Denunciar

0

I think it should be possible to do so

let a = ['a','b','c','d'];
let b = [1,2,3,4];
let tempArray = [];
for(let i = 0;i<a.length;i++){
    
    let tempAttribute = {
        'name' : b[i],
        'value' : a[i]
    }
    tempArray.push(tempAttribute)
}
console.log(tempArray);

Because the subscript of the data is fixed, we can use its subscript to do something, you can also use other looping methods.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can do:

const firstArray = ['a','b','c','d']
const secondArray = [1,2,3,4]

const result = firstArray.map((letter, index) => ({
  name: letter, 
  value: secondArray[index].toString()
}))

console.log(result)

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