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

118
Vistas
Convert Javascript array to JSON with user defined key

Is there a clean efficient method of converting a basic 1 dimensional array such as:

arrayEx = [1,2,3,4]

into a user defined JSON array such as:

jsonArrayEx = [{"exampleKey": 1}, {"exampleKey": 2}, {"exampleKey": 3}, {"exampleKey": 4}]

I have seen examples on using JSON.Stringify where the key is based on the index of the array but nothing with the key defined by the user as shown in my example.

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

0

You can use Array#map with a computed property name when creating a new object.

const arr = [1,2,3,4];
let key = "exampleKey";
const res = arr.map(x => ({[key]: x}));
console.log(res);

about 4 years ago · Juan Pablo Isaza Denunciar

0

function mapper(key){ return JSON.stringify(arrayEx.map(a => ({[key]: a}));}

about 4 years ago · Juan Pablo Isaza Denunciar

0

let arr= [1,2,3,4];
let newArr = [];
for(let i of arr){
  newArr.push({"exampleKey" : i});
}
 
  console.log(newArr);

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