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
Make React Useeffect Hook Not Run on Initial Render
Why How to Use a Function Before It's Defined in JavaScript
How to Implement Dynamic Getters/Setters in JavaScript
How to Make a JSONp Request from JavaScript Without Jquery
Request Header Field Access-Control-Allow-Headers Is Not Allowed by Access-Control-Allow-Headers
How to Change CSS :Root Color Variables in JavaScript
How to Format/Tidy/Beautify in JavaScript
Jquery $("#Radiobutton").Change(...) Not Firing During De-Selection
How to Convert HTML to JSON Using PHP
Fast Rectangle to Rectangle Intersection
How to Record Webcam and Audio Using Webrtc and a Server-Based Peer Connection
Add/Delete Table Rows Dynamically Using JavaScript