Categories
JavaScript Answers

How to extract the property values of a JavaScript object into an array?

Spread the love

Sometimes, we want to extract the property values of a JavaScript object into an array.

In this article, we’ll look at how to extract the property values of a JavaScript object into an array.

How to extract the property values of a JavaScript object into an array?

To extract the property values of a JavaScript object into an array, we use the Object.values method.

For instance, we write

const dataObject = {
  object1: {
    id: 1,
    name: "Fred",
  },
  object2: {
    id: 2,
    name: "Wilma",
  },
  object3: {
    id: 3,
    name: "Pebbles",
  },
};

const valuesOnly = Object.values(dataObject);

to call Object.values with dataObject to return the property values of dataObject in an array.

Conclusion

To extract the property values of a JavaScript object into an array, we use the Object.values method.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *