During development we often came across a need of conversion an array to an object.
In this article, lets see how to convert, JavaScript array to an object quickly using widely popular and accepted spread operator (...).
Example
var fruits = ["apple", "grapes", "mangoe", "orange"];
var fruitsObject = { ...fruits };
console.table(fruitsObject);
Output for console.table
| (index) | Value |
|---|---|
| 0 | ”apple” |
| 1 | ”grapes” |
| 2 | ”mango” |
| 3 | ”orange” |
Hope you like this!
Keep helping and happy 😄 coding
With ❤️ from 🇮🇳
Related Articles
Deepen your understanding with these curated continuations.
How to Update an Array Element in JavaScript
Learn the simplest ways to update array elements in JavaScript. This guide explains how to use assignment operators and modern methods to modify array values.
Flatten an Array One Level Deep in Javascript
This article explains to flatten an array one level deep in javascript comparing with lodash _.flatten method.
Identify whether device is a mobile or desktop
This article explains to flatten an array one level deep in javascript comparing with lodash _.flatten method.