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

Cover Image for How to convert an Object {} to an Array [] of key-value pairs in JavaScript
Matheus Mello
Matheus Mello
published a few days ago. updated a few hours ago

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! ✌️


More Stories

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

How can I echo a newline in a batch file?

updated a few hours ago
batch-filenewlinewindows

🔥 💻 🆒 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

Matheus Mello
Matheus Mello
Cover Image for How do I run Redis on Windows?

How do I run Redis on Windows?

updated a few hours ago
rediswindows

# 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

Matheus Mello
Matheus Mello
Cover Image for Best way to strip punctuation from a string

Best way to strip punctuation from a string

updated a few hours ago
punctuationpythonstring

# 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

Matheus Mello
Matheus Mello
Cover Image for Purge or recreate a Ruby on Rails database

Purge or recreate a Ruby on Rails database

updated a few hours ago
rakeruby-on-railsruby-on-rails-3

# 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

Matheus Mello
Matheus Mello