Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

85
Views
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 answers
Answer question

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 Report

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!