Javascript convert object value to array

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:

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:

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:

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.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe