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

127
Views
Best way to convert array to object

What is the best way to convert this

const items = [
  { name: "Leon", url: "../poeple" },
  { name: "Bmw", url: "../car" }
];

into this using javascript :

const result = {
  Leon: "../poeple",
  Bmw: "../car"
};
about 4 years ago · Juan Pablo Isaza
3 answers
Answer question

0

You could map the objects as entries and get an object from it.

const
    items = [{ name: "Leon", url: "../poeple" }, { name: "Bmw", url: "../car" }],
    result = Object.fromEntries(items.map(({ name, url }) => [name, url]));

console.log(result);

about 4 years ago · Juan Pablo Isaza Report

0

You could just reduce the array:

const items = [{
    name: "Leon",
    url: "../people"
  },
  {
    name: "Bmw",
    url: "../car"
  }
];

const o = items.reduce((o, el) => {
  o[el.name] = el.url;
  return o;
}, {});

console.log(o)

The Array.prototype.reduce method is very flexible, and might be used to reduce an array to another entity:

The reduce() method executes a user-supplied "reducer" callback function on each element of the array, in order, passing in the return value from the calculation on the preceding element. The final result of running the reducer across all elements of the array is a single value.

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/reduce

about 4 years ago · Juan Pablo Isaza Report

0

There are lots of different approaches as your can see. But if you're new to JavaScript maybe a simple loop over the array to create new keys and values in a new object would be easier to understand.

const items=[{name:"Leon",url:"../poeple"},{name:"Bmw",url:"../car"}];

const out = {};

for (const obj of items) {
  out[obj.name] = obj.url;
}

console.log(out);

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!