How to convert an Object {} to an Array [] of key-value pairs in JavaScript

Matheus Mello
Matheus Mello
September 2, 2023
Cover Image for How to convert an Object {} to an Array [] of key-value pairs in JavaScript

Converting an Object to an Array of Key-Value Pairs in JavaScript 💻

So, you have an object and you want to convert it into an array of key-value pairs in JavaScript? No worries! I've got you covered. 🤩

The Problem 🧐

Let's say you have an object like this:

const myObj = {"1": 5, "2": 7, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0, "11": 0, "12": 0};

And you want to convert it into an array of key-value pairs like this:

[[1, 5], [2, 7], [3, 0], [4, 0], [5, 0], [6, 0], [7, 0], [8, 0], [9, 0], [10, 0], [11, 0], [12, 0]]

The Solution ✅

There are a couple of approaches you can take to solve this problem. Let's explore two simple and effective ways! 👇

1. Using the Object.entries() Method 🗝

JavaScript provides us with the handy Object.entries() method which can be used to convert an object into an array of key-value pairs.

Here's how it works:

const myObj = {"1": 5, "2": 7, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0, "11": 0, "12": 0};

const keyValueArray = Object.entries(myObj);

console.log(keyValueArray);

Running the code will output:

[
  ["1", 5],
  ["2", 7],
  ["3", 0],
  ["4", 0],
  ["5", 0],
  ["6", 0],
  ["7", 0],
  ["8", 0],
  ["9", 0],
  ["10", 0],
  ["11", 0],
  ["12", 0]
]

2. Using a for...in Loop 🔄

Another approach is to use a for...in loop to iterate over the object's keys and push each key-value pair into a new array.

Here's the code:

const myObj = {"1": 5, "2": 7, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0, "11": 0, "12": 0};

const keyValueArray = [];

for (let key in myObj) {
  keyValueArray.push([key, myObj[key]]);
}

console.log(keyValueArray);

This will also output:

[
  ["1", 5],
  ["2", 7],
  ["3", 0],
  ["4", 0],
  ["5", 0],
  ["6", 0],
  ["7", 0],
  ["8", 0],
  ["9", 0],
  ["10", 0],
  ["11", 0],
  ["12", 0]
]

Time to Convert! ⏱️

Now that you know two easy ways to convert an object into an array of key-value pairs in JavaScript, you can confidently tackle this common problem in your code. 🚀

Would you like to learn more awesome JavaScript tips and tricks? Check out my blog and let's level up your coding skills together! 😎

Share this post with your fellow developers who might find it helpful. And don't forget to leave a comment below sharing your favorite method or any other creative solutions you've come up with. Let's keep the conversation going! 💬💪

Happy coding! ✌️

Take Your Tech Career to the Next Level

Our application tracking tool helps you manage your job search effectively. Stay organized, track your progress, and land your dream tech job faster.

Your Product
Product promotion

Share this article

More Articles You Might Like

Latest Articles

Cover Image for How can I echo a newline in a batch file?
batch-filenewlinewindows

How can I echo a newline in a batch file?

Published on March 20, 2060

🔥 💻 🆒 Title: "Getting a Fresh Start: How to Echo a Newline in a Batch File" Introduction: Hey there, tech enthusiasts! Have you ever found yourself in a sticky situation with your batch file output? We've got your back! In this exciting blog post, we

Cover Image for How do I run Redis on Windows?
rediswindows

How do I run Redis on Windows?

Published on March 19, 2060

# Running Redis on Windows: Easy Solutions for Redis Enthusiasts! 🚀 Redis is a powerful and popular in-memory data structure store that offers blazing-fast performance and versatility. However, if you're a Windows user, you might have stumbled upon the c

Cover Image for Best way to strip punctuation from a string
punctuationpythonstring

Best way to strip punctuation from a string

Published on November 1, 2057

# The Art of Stripping Punctuation: Simplifying Your Strings 💥✂️ Are you tired of dealing with pesky punctuation marks that cause chaos in your strings? Have no fear, for we have a solution that will strip those buggers away and leave your texts clean an

Cover Image for Purge or recreate a Ruby on Rails database
rakeruby-on-railsruby-on-rails-3

Purge or recreate a Ruby on Rails database

Published on November 27, 2032

# Purge or Recreate a Ruby on Rails Database: A Simple Guide 🚀 So, you have a Ruby on Rails database that's full of data, and you're now considering deleting everything and starting from scratch. Should you purge the database or recreate it? 🤔 Well, my