Languages
[Edit]
EN

JavaScript - convert array to object with values as keys

3 points
Created by:
a_horse
538

In this article, we would like to show you how to convert array to object with array values as keys working with JavaScript.

Quick solution:

or: 

Warning: the solution based on spread operator is not recommended because of bad time performance.

 

Practical examples

Edit

In this examples, we create a reusable function that converts the array to object with the array values as keys using reduce() method.

1. Assigning property based example

Edit

2. Spread operator based example

Edit

Warning: the solution based on spread operator is not recommended because of bad time performance.

 

See also

Edit
  1. JavaScript - convert array to object

References

Edit
  1. Spread syntax (...) - JavaScript | MDN

Alternative titles

  1. JavaScript - convert array to object with array values as keys
1
Donate to Dirask
Our content is created by volunteers - like Wikipedia. If you think, the things we do are good, donate us. Thanks!
Join to our subscribers to be up to date with content, news and offers.
Native Advertising
🚀
Get your tech brand or product in front of software developers.
For more information Contact us
Dirask - we help you to
solve coding problems.
Ask question.

❤️💻 🙂

Join