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

145
Views
How to swap keys and values in an object? JS

Please tell me how to get such an array where id : newExcluded(key), and excluded :(newExcluded value )

  const newExcluded = {1:true,6:false,3:false,4:true,7:true}
    
     coonst fetchExcluded = [
        {"id": 1,"excluded": true},
        {"id": 6, "excluded": false},
        {"id": 3, "excluded": false},
        {"id": 4, "excluded": true},
        {"id": 7, "excluded": true},
       ]
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You can use a Object.entries to convert it to an array of arrays and Array.prototype.map to convert each inner array to an object with the required properties

const newExcluded = {1:true,6:false,3:false,4:true,7:true}

fetchExcluded = Object.entries(newExcluded).map(([id, excluded]) => ({ id, excluded }));

console.log(fetchExcluded);

about 4 years ago · Juan Pablo Isaza Report

0

Without using map.

Iterate over the Object.entries (this returns an array which is why we're using for/of), and assign the key/value of each property to a new object.

const obj = {1:true,6:false,3:false,4:true,7:true};

const output = {};

for (const [key, value] of Object.entries(obj)) {
  output[key] = value;
}

console.log(output);

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!