Pick Random Property from a JavaScript Object

Pick random property from a Javascript object

The chosen answer will work well. However, this answer will run faster:

var randomProperty = function (obj) {
var keys = Object.keys(obj);
return obj[keys[ keys.length * Math.random() << 0]];
};

How to obtain a random property from an object inside an array object?

Already answered here.

const keys = Object.keys(questionsObject);
const randomQuestion = questionsObject[keys[keys.length * Math.random() << 0]];

Select a random object in javascript/jquery

A quick solution using plain javascript would be generate a random number and use it using Object.keys().

var keys = Object.keys(choose_one);
var randomKey = key[Math.floor(Math.random()*keys.length)];
var value = choose_one[randomKey];

randomly select value from a JavaScript object

If you put the object's keys in an array then you can easily select one at random, maybe a little something like this:

var student = { name : "John Doe", age : "28", gender : "Male" };
var keys = Object.keys(student);var randomKey = keys[Math.floor(Math.random()*keys.length)];var randomValue = student[randomKey];
document.getElementById("propertydiv").innerHTML = randomKey;document.getElementById("valuediv").innerHTML = randomValue;
<div id="propertydiv"></div><div id="valuediv"></div>

How to get a random key value from a JavaScript object

Probably you can use Object.keys() instead.

Try the following:

const phrases = {  hola: "hello",  adios: "bye",};
const keys = Object.keys(phrases);const len = keys.length;const rnd = Math.floor(Math.random() * len);const key = phrases[keys[rnd]];
console.log(key);

Loop through object and get random item in javascript

The use of Math.random() in David's answer is wrong : you could end up with keys[-1]! The correct way is to use Math.floor(Math.random()*length). Moreover I would use the Object.keys() method to be as general as you need, see this doc. Here is a working solution:

const list = {  b1: {    author: 'Mozart',    title: 'lacrimosa',    text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',    imageURL: 'url(./assets/img/image.jpg)'  },  b2: {    author: 'Chopin',    title: 'Waltz in A minor',    text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',    imageURL: 'url(./assets/img/image.jpg)'  },  b3: {    author: 'Bach',    title: 'Ave Maria',    text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',    imageURL: 'url(./assets/img/image.jpg)'  },};
const keys = Object.keys(list);const randomIndex = keys[Math.floor(Math.random() * keys.length)];const item = list[randomIndex];console.log(item.author);

Randomly calling an object property

Will something like this work? You can keep your monsters as an object and use Object.keys to fetch your keys (in this case your monsters) as an array. Then it's just a matter of plucking out a random monster with Math.random:

// so you have some object where the keys are your monsters
var monsters = { ghost: '..', skeleton: '..', donald_trump: '..'};

// grab your monsters above as an array
var monsterArray = Object.keys(monsters);

// pick your monster at random
var randomKey = Math.floor(Math.random() * monsterArray.length);

console.log('holy crap we found a ' + monsterArray[randomKey]);


Related Topics



Leave a reply



Submit