Emmanuel Akinfulubi

JavaScriptObject

Get all values of an object in JavaScript

Beginner · 3 min read

Sometimes you don't care about an object's keys, only what is stored under them. You might want every price in a price list to add them up, every score in a scoreboard to find the highest, or every answer in a form to check none is empty.

In this tutorial you will get all the values of a student object as an array. The answer is one line, so we will spend the rest of the lesson on what it guarantees: the order the values come out in, and which values it leaves out. You need to know objects, arrays and map.

Keys, values and entries

Every property is a pair: a key and a value. JavaScript has three matching methods to pull them out:

JavaScript

const student = { name: 'David Rayy', sclass: 'VI', rollno: 12 };
console.log(Object.keys(student));    // [ 'name', 'sclass', 'rollno' ]
console.log(Object.values(student));  // [ 'David Rayy', 'VI', 12 ]
console.log(Object.entries(student)); // [ [ 'name', 'David Rayy' ], [ 'sclass', 'VI' ], [ 'rollno', 12 ] ]

entries gives both at once, as little [key, value] arrays.

Here is the exercise. Try it yourself first if you like, or read on and we will build it together.

14 Get all the values of an object

easy

Return the values rather than the keys, and be aware of what order they come out in.

Stuck? Three hints, smallest first
  1. 1. Object.values is the direct answer.
  2. 2. The order matches Object.keys exactly, so the two line up index by index.
  3. 3. Nothing removes duplicates, because two keys may hold the same value.

Step 1: the direct answer

Step 1

const valuesOf = (obj) => Object.values(obj);
console.log(valuesOf(student)); // [ 'David Rayy', 'VI', 12 ]
console.log(valuesOf({}));      // []

Object.values reads the object's own properties and returns their values in a new array. An empty object gives an empty array.

Step 2: build it by hand

Before Object.values existed, people got the keys and looked each one up. It is worth writing once, because it shows what Object.values does for you:

Step 2

const valuesByMap = (obj) => Object.keys(obj).map((k) => obj[k]);
console.log(valuesByMap(student)); // [ 'David Rayy', 'VI', 12 ]

obj[k] with square brackets reads the property whose name is in the variable k. obj.k would look for a property literally called "k".

Step 3: the order lines up

Object.keys, Object.values and Object.entries always list properties in the same order. So position 0 of the keys belongs with position 0 of the values:

Step 3

const keys = Object.keys(student);
const values = Object.values(student);
console.log(keys[2], values[2]); // rollno 12

That guarantee is what lets you turn the entries back into the same object:

JavaScript

console.log(Object.fromEntries(Object.entries(student))); // { name: 'David Rayy', sclass: 'VI', rollno: 12 }

Step 4: number-like keys come first

The order is the order you added the keys, with one exception. Keys that look like whole numbers move to the front:

Step 4

console.log(valuesOf({ b: 'second', 1: 'first' })); // [ 'first', 'second' ]

b was written first, but the key 1 is listed first, so its value comes first too.

Step 5: inherited values are left out

Object.create(proto) makes an object that borrows from proto. The borrowed property is readable, but it isn't the object's own:

Step 5

const withInherited = Object.create({ inherited: 'not mine' });
withInherited.own = 'mine';
console.log(withInherited.inherited); // not mine
console.log(valuesOf(withInherited)); // [ 'mine' ]

Object.values only lists own properties, just like Object.keys.

Step 6: duplicates and undefined stay

Nothing is removed from the list. Two keys can hold the same value, and a key can hold undefined:

Step 6

console.log(valuesOf({ a: 1, b: 1, c: 2 })); // [ 1, 1, 2 ]
console.log(valuesOf({ a: undefined }));     // [ undefined ]

If you want each value once, wrap the result in a Set: [...new Set(valuesOf(obj))].

The full solution

The panel prints the values, keys and entries of the student, the inherited case, and the duplicate case. Press Run to see them all.

The answer

const student = { name: 'David Rayy', sclass: 'VI', rollno: 12 };

const valuesOf = (obj) => Object.values(obj);

// Before Object.values existed, this was the way. Still useful for the shape.
const valuesByMap = (obj) => Object.keys(obj).map((k) => obj[k]);

const withInherited = Object.create({ inherited: 'not mine' });
withInherited.own = 'mine';

console.log('values :', valuesOf(student));
console.log('keys   :', Object.keys(student));
console.log('entries:', Object.entries(student));
console.log('inherited value is skipped:', valuesOf(withInherited), '<- own only');
console.log('but readable:', withInherited.inherited);
console.log('duplicates are kept:', valuesOf({ a: 1, b: 1, c: 2 }));

Output


      
12 checks that prove this answer

You do not have to read these. They are part of the answer panel above, so pressing Run there executes all 12 of them and prints ok for each. That output is the proof, and the build fails if a single check fails.

12 checks

test('the values, in key order', valuesOf(student), ['David Rayy', 'VI', 12]);
test('the manual version agrees', valuesByMap(student), valuesOf(student));
test('there are as many values as keys', valuesOf(student).length, Object.keys(student).length);
test('values line up with keys index by index', Object.keys(student).map((k) => student[k]), valuesOf(student));
test('an empty object gives an empty array', valuesOf({}), []);
test('inherited values are not included', valuesOf(withInherited), ['mine']);
test('even though the property is readable', withInherited.inherited, 'not mine');
test('duplicate values are all kept', valuesOf({ a: 1, b: 1, c: 2 }), [1, 1, 2]);
test('a value of undefined is still a value', valuesOf({ a: undefined }), [undefined]);
test('numeric keys reorder the values too', valuesOf({ b: 'second', 1: 'first' }), ['first', 'second']);
test('entries pairs them up', Object.entries({ a: 1 }), [['a', 1]]);
test('and fromEntries reverses it', Object.fromEntries(Object.entries(student)), student);

Object.values is a one-liner, so the value of this exercise is the guarantee behind it: Object.keys, Object.values and Object.entries all enumerate in the same order, so index i of one lines up with index i of the others. That is what makes Object.fromEntries(Object.entries(obj)) a faithful shallow copy. Note the numeric-key assertion again — { b: 'second', 1: 'first' } yields ['first', 'second'], because integer-like keys are enumerated first.

Common mistakes

  • Writing obj.k inside the map. That reads a property named "k". Use obj[k].
  • Expecting inherited values. Object.values only lists the object's own properties.
  • Assuming the values are unique. Two keys can hold the same value, and both are kept.

Try it yourself

  1. Add up all the values of { rent: 1200, food: 450, travel: 300 }.
  2. Find the highest value in { ada: 82, tunde: 91, chi: 77 } with Math.max(...).
  3. Check whether any value in an object is an empty string, using some.

Keep practising

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.