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

87
Vistas
Create JSON dynamically with dynamic keys and values in Express Js

I am fetching API into my Express server which has several JSON key value pairs in one array. For Example:

[{
 "quality": "best",
 "url": "https://someurlhere.example/?someparameters"
},
{
 "quality": "medium",
 "url": "https://someurlhere1.example/?someparameters"
}]

And I want to create an array of JSON of that received data in this Format:

[{
"best": "https://someurlhere.example/?someparameters"
},
{
"medium": "https://someurlhere1.example/?someparameters"
}]

I have tried doing this by using for loop

for(let i=0; i < formats.length; i++){
   arr.push({
     `${formats[i].quality}` : `${formats[i].url}`
   })
}

But it didn't work for me.

Please help me in achieving this. Thanks in Advance :)

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

0

You could use the map function and create a new object from it.

For example:

let prevArr = [{
  "quality": "best",
  "url": "https://someurlhere.example/?someparameters"
}, {
  "quality": "medium",
  "url": "https://someurlhere1.example/?someparameters"
}]; // Replace with your array
let newArr = [];
let obj = {};

prevArr.map(function(x) {
  obj = {};
  obj[x["quality"]] = x.url;
  newArr.push(obj);
});
about 4 years ago · Juan Pablo Isaza Denunciar

0

const input = [{
    "quality": "best",
    "url": "https://someurlhere.example/?someparameters"
}, {
    "quality": "medium",
    "url": "https://someurlhere1.example/?someparameters"
}];
const result = input.map((v, i) => {
    return {
        [v["quality"]]: v["url"]
    }
});
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