Borislav Hadzhiev
Thu Oct 28 2021ยท2 min read
Photo by Kace Rodriguez
To get a random property from an object:
Object.keys()
method to get an array of the object's keys.Math.floor()
and Math.random()
functions to get a random index of
the array.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.
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.
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
.
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
.
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.
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.
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