3 Ways to Convert an Array to an Object in NodeJS

3 Ways to Convert an Array to an Object in NodeJS

Do you need to convert an array with values to an object? In this article, we'll cover the three ways you can go about doing this with Nodejs

Let's get started!

Table of Contents

  1. Object.assign()
  2. Loop Over Array & Construct a New Object
  3. Reduce()

1. Object.assign()

Object.assign() is the first method we'll cover. This method is used to copy values from one or more source objects to a target object.

 const array = ["Johnny", "Billy", "Sandy"]

const newObject = Object.assign({}, array)

// newObject = { "0": "Johnny", "1": "Billy", "2": "Sandy" }

2. Loop Over Array & Construct a New Object

In this method, we're going to loop over each item in your array and add each one as a new property in a new object.

Here's the full code:

const array = ["Johnny", "Billy", "Sandy"]

let newObject = {}

for (let i=0; i < array.length; i++) {
  if (array[i] !== undefined) {
    newObject[i] = array[i]
  }
}

// newObject = { "0": "Johnny", "1": "Billy", "2": "Sandy" }

First, we declare an empty object called newObject that will hold the properties we will move over from the array.

Then, we use a for loop to iterate over each value in the array. If the value doesn't have an undefined value, we add the value as a new property to the newObject.

3. Reduce()

The last example is the reduce() method. Reduce() method executes a provided function for each value of the array and the return values are stored in an accumulator, which is an object in this case.

const array = ["Johnny", "Billy", "Sandy"]

const newObject = array.reduce(function(result, item, index) {
  result[index] = item
  return result
}, {})

// newObject = { "0": "Johnny", "1": "Billy", "2": "Sandy" }

Conclusion

Did we miss a method you use? Let us know in the comments.

Thanks for reading and happy coding!

If you liked this post, share it with all of your programming buddies!

Originally published by Nick Major at https://coderrocketfuel.com

node-js arrays

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Hire Node.js Developers And How Much Does It Cost?

A Guide to Hire Node.js Developers who can help you create fast and efficient web applications. Also, know how much does it cost to hire Node.js Developers.

Hire Node.JS Developers | Skenix Infotech

We are providing robust Node.JS Development Services with expert Node.js Developers. Get affordable Node.JS Web Development services from Skenix Infotech.

Hands on with Node.Js Streams | Examples & Approach

The practical implications of having Streams in Node.js are vast. Nodejs Streams are a great way to handle data chunks and uncomplicate development.

Node.js Performance: Node.js vs. Io.js

You may already be aware that Raygun uses Node.JS for our API nodes that receive your precious crash reporting data (we also do node.js crash reporting if you’re interested). We’ve peaked in the past at more than 110,000 requests per second coming...

Node.js Live | Node.js Docker Tutorial | Dockerizing Node.js App|Node.js Training|Edureka

🔥 Node.js Certification Training: https://www.edureka.co/nodejs-certification-training This Edureka video on 'Node.js Docker Tutorial' will help you in learn...