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

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

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 Relatório

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 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