How to Convert Array to Object in Javascript?

What is Array Object ?

JavaScript, arrays are a special type of object used for storing and manipulating lists of values. Despite their name, arrays in JavaScript are more flexible than traditional arrays in other languages, as they can hold a mixture of data types and can dynamically resize. You can think of an array in JavaScript as an object with a special structure optimized for handling sequential data.

There are two main ways to convert an array to an object in JavaScript:

  1. Using the Object.assign() method: The Object.assign() method takes one or more source objects and copies their properties to a target object. To convert an array to an object using the Object.assign() method, we can pass the array as the source object and an empty object as the target object.
const arr = [1, 2, 3];
const obj = Object.assign({}, arr);

console.log(obj); // {0: 1, 1: 2, 2: 3}

Using the Object.fromEntries() method: The Object.fromEntries() method takes an iterable of key-value pairs and returns an object with those properties. To convert an array to an object using the Object.fromEntries() method, we can use the Array.prototype.map() method to create an iterable of key-value pairs from the array.

const arr = [["name", "John Doe"], ["age", 30]];
const obj = Object.fromEntries(arr.map(([key, value]) => [key, value]));

console.log(obj); // {name: "John Doe", age: 30}
Hi I am Amit Kumar Thakur Experienced as s Software Developer with a demonstrated history of working in the information technology and services industry. Skilled in HTML, CSS, Bootstrap4, PHP, Laravel-9 , REST API,FB API,Google API, Youtube Api, Bitbucket,Github,Linux and jQuery. Strong engineering professional focused in Computer/Information Technology Administration and Management. Currently my profile is to Software Developer, analyze the requirement, creating frame for web application, coding and maintenance.

Related Posts

How to use Global Variable using JavaScript in Laravel ?

What is Global Variable ? A global variable is a variable that is defined and accessible throughout the entire program or a significant portion of it, rather…

How to print Object Value using Array in Javascript ?

In this tutorial i’m going to print the object value using javascript ? First go to controller function and put below code. Next go to blade page…

How to remove fake path from url in laravel ?

In this tutorial we’re going to learn how to remove fake path from url in laravel using javascript. Now fakepath removing successfully form url 👍

List of My Blogs Written from May to October 2023

List of My Blogs Written in May 2023 1 How to Show Notification using Sweet Alert in Laravel 9 2 How to use Soft Delete and Restore…

What is Model ? Object Model in JavaScript ?

What is Model ? A model is a representation of a real-world entity or concept. It can be used to describe the structure and behavior of the…

How to print Null value Using JavaScript ?

In JavaScript, if you want to print the string “null” when a variable is null, you can do so add in !== operator. Benefits of Using if-else…

0 0 votes
Article Rating
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x