Skip to content
the poor coder

Javascript convert object value to array

Poor Man
Published
Reading time
2 min read
On this page

Javascript: Converting Object Value to Array#

As a programmer, I often come across situations where I need to convert an object's values into an array. This can be a bit tricky, but fortunately, JavaScript provides several ways to accomplish this task.

Method 1: Using Object.values()#

One of the easiest ways to convert an object's values into an array is by using the Object.values() method. This method returns an array containing all the values of an object's properties in the order they appear in the object:

Text
const myObject = {name: "John", age: 30, city: "New York"};

const myArray = Object.values(myObject);

console.log(myArray); // Output: ["John", 30, "New York"]

In the above code, we have created an object named myObject with three properties. We then used the Object.values() method to convert the values of the object into an array and stored it in a variable named myArray. Finally, we logged the contents of the myArray variable to the console.

Method 2: Using a for...in Loop#

Another way to convert an object's values into an array is by using a for...in loop. This loop iterates through all the properties of an object and pushes their values into an array:

Text
const myObject = {name: "John", age: 30, city: "New York"};

const myArray = [];

for (let property in myObject) {
  if (myObject.hasOwnProperty(property)) {
    myArray.push(myObject[property]);
  }
}

console.log(myArray); // Output: ["John", 30, "New York"]

In the above code, we have created an object named myObject with three properties. We then created an empty array named myArray and used a for...in loop to iterate through all the properties of the object. Inside the loop, we check if the property is a direct property of the object (not inherited from its prototype) using the hasOwnProperty() method. If so, we push its value into the myArray array. Finally, we logged the contents of the myArray variable to the console.

Method 3: Using Object.entries()#

The Object.entries() method returns an array of an object's key-value pairs. We can then use the map() method to extract only the values and store them in an array:

Text
const myObject = {name: "John", age: 30, city: "New York"};

const myArray = Object.entries(myObject).map(([key, value]) => value);

console.log(myArray); // Output: ["John", 30, "New York"]

In the above code, we have created an object named myObject with three properties. We then used the Object.entries() method to get an array of key-value pairs of the object. We then used the map() method to extract only the values from the key-value pairs and store them in a new array named myArray. Finally, we logged the contents of the myArray variable to the console.

Conclusion#

Converting an object's values into an array is a common task in JavaScript programming. We can achieve this using various methods such as Object.values(), for...in loop, and Object.entries() method. By understanding these methods, we can easily manipulate data in our JavaScript programs.

Keep going

2023

how to sort an array

How to Sort an Array If you have an array and you want to order its elements in a specific way, you need to use a sorting algorithm. There are several sorting algorithms available, but two of the most commonly used are…

javascript2 min read
2023

jquery mobile or desktop

jQuery Mobile or Desktop? As a web developer, I have had some experience with both jQuery Mobile and jQuery Desktop. Both frameworks have their pros and cons, and which one to use really depends on the specific project…

javascript1 min read
2023

node.js folder structure

Node.js Folder Structure If you are new to Node.js, it can be daunting to create a folder structure that will work for your project. The structure you choose will depend on the size and complexity of your project. Here…

javascript1 min read
ESC