Get a Random Property from an Object in JavaScript

avatar

Borislav Hadzhiev

Last updated: Oct 28, 2021

banner

Photo from Unsplash

Get a Random Property from an Object #

To get a random property from an object:

  1. Call the Object.keys() method to get an array of the object's keys.
  2. Use the Math.floor() and Math.random() functions to get a random index of the array.
  3. Use the random index to access one of the object's properties.
index.js
const obj = { name: 'Tom', country: 'Chile', age: 30, }; function getRandomProperty(obj) { const keys = Object.keys(obj); return keys[Math.floor(Math.random() * keys.length)]; } console.log(getRandomProperty(obj)); // 👉️ name console.log(getRandomProperty(obj)); // 👉️ age console.log(getRandomProperty(obj)); // 👉️ name

We created a reusable function that returns a random property from an object.

The first step is to get the an array of the object's keys using the Object.keys method.

index.js
const obj = { name: 'Tom', country: 'Chile', age: 30, }; // 👇️ ['name', 'country', 'age'] console.log(Object.keys(obj));

The Math.floor function, rounds a number down if the number has a decimal, otherwise it returns the number as is.

index.js
console.log(Math.floor(2.99)); // 👉️ 2 console.log(Math.floor(2.01)); // 👉️ 2 console.log(Math.floor(2)); // 👉️ 2

The Math.random function returns a random number in the range 0 to less than 1. The function could return 0, but it could never return 1.

index.js
console.log(Math.random()); // 👉️ 0.1034543... console.log(Math.random()); // 👉️ 0.5438454... console.log(Math.random()); // 👉️ 0.5438483... // 👇️ Multiplied by keys.length const keys = ['name', 'country', 'age']; console.log(Math.random() * keys.length); // 👉️ 2.534... console.log(Math.random() * keys.length); // 👉️ 1.543... console.log(Math.random() * keys.length); // 👉️ 1.754... console.log(Math.random() * keys.length); // 👉️ 0.324...

The max value we could get is, if Math.random returns 0.999... and we multiply it by the array's length (3). We would get back a value of 2.997.

index.js
console.log(0.999 * 3); // 👉️ 2.997

However, because indexes are zero-based in JavaScript, the last element in the array has an index of array.length - 1. For this reason, we used the Math.floor() function to round the number down to the next integer, which is 2 (the last index in the array of keys).

Conversely, the min value we could get is, if Math.random() returns 0. In this case, we would multiply 0 by the array's length and get 0 back.

index.js
console.log(0 * 3); // 👉️ 0

Calling the Math.floor function with 0 returns the number as is. In this scenario, we would access the first element in the keys array.

The expression returns an index from 0 up to (but not including) array.length. This guarantees us that we could never access an index of out bounds.

Here's the complete code snippet.

index.js
const obj = { name: 'Tom', country: 'Chile', age: 30, }; // 👇️ ['name', 'country', 'age'] console.log(Object.keys(obj)); function getRandomProperty(obj) { const keys = Object.keys(obj); return keys[Math.floor(Math.random() * keys.length)]; } console.log(getRandomProperty(obj)); // 👉️ country console.log(getRandomProperty(obj)); // 👉️ age console.log(getRandomProperty(obj)); // 👉️ country

Further Reading #

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.