How to Get Values of a Javascript Object with Object.values()

Use-Cases of this Tutorial

  • Know how to get all values of a Javascript object.
  • Know about the Object.values() method.

All property values of a Javascript object can be found with the Object.values() method.

Object.values()

Object.values() returns an array of all values of object's own enumerable property names.

Enumerable properties of an object have their "enumerable" attribute set to "true" (by default this is "true" when a new property is added to the object).

Own enumerable properties are owned by the object and not by an other object in its prototype chain (In Javascript inheritance happens through prototypes).

Example #1 :

var student = {
	name: "John Wick",
	age: 27,
	gender: "M"
};

var values = Object.values(student);

// [ "John Wick", 27, "M" ]
console.log(values);

Example #2 :

// create objects using constructor function
function Student(name, age, gender) {
	this.name = name;
	this.age = age;
	this.gender = gender;
}

Student.prototype = {
	city: "Pisa",
	country: "Italy"
};

// new object
var student_1 = new Student("John Wick", 27, "M");

var values = Object.values(student_1);

console.log(values);

// [ "John Wick", 27, "M" ]
// "Pisa" and "Italy" will not be included as the belong to its prototype