Loop through object and get random item in javascript [duplicate]
This question already has an answer here:
randomly select value from a JavaScript object
2 answers
Suppose you have a Javascript object with some properties to display a page with background a text and a title randomly how would you loop inside it to make it display only one item on the object each time?
const list = {
b1:{
author: 'Mozart',
title: 'lacrimosa',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
imageURL: 'url(./assets/img/image.jpg)'
},
b2:{
author: 'Choppin',
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)'
},
}
return(
<div className="background" style={{background: 'url(./assets/img/image.jpg)'}}>
<div className="caption">
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p> artist - <cite>Song</cite></p>
</blockquote>
</div>
</div>
)
I tried the Math.random() method but it didn't work
javascript arrays reactjs loops object
marked as duplicate by user3297291, Community♦ Nov 26 '18 at 1:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
randomly select value from a JavaScript object
2 answers
Suppose you have a Javascript object with some properties to display a page with background a text and a title randomly how would you loop inside it to make it display only one item on the object each time?
const list = {
b1:{
author: 'Mozart',
title: 'lacrimosa',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
imageURL: 'url(./assets/img/image.jpg)'
},
b2:{
author: 'Choppin',
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)'
},
}
return(
<div className="background" style={{background: 'url(./assets/img/image.jpg)'}}>
<div className="caption">
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p> artist - <cite>Song</cite></p>
</blockquote>
</div>
</div>
)
I tried the Math.random() method but it didn't work
javascript arrays reactjs loops object
marked as duplicate by user3297291, Community♦ Nov 26 '18 at 1:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Do you want to render random element fromlist
object ?
– t1m0n
Nov 16 '18 at 9:27
1
Have a look at this question that explains how to get a random value from an object.
– user3297291
Nov 16 '18 at 9:27
Do you have any code other than an object, like a function that usesMath.random
?
– zer00ne
Nov 16 '18 at 9:29
add a comment |
This question already has an answer here:
randomly select value from a JavaScript object
2 answers
Suppose you have a Javascript object with some properties to display a page with background a text and a title randomly how would you loop inside it to make it display only one item on the object each time?
const list = {
b1:{
author: 'Mozart',
title: 'lacrimosa',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
imageURL: 'url(./assets/img/image.jpg)'
},
b2:{
author: 'Choppin',
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)'
},
}
return(
<div className="background" style={{background: 'url(./assets/img/image.jpg)'}}>
<div className="caption">
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p> artist - <cite>Song</cite></p>
</blockquote>
</div>
</div>
)
I tried the Math.random() method but it didn't work
javascript arrays reactjs loops object
This question already has an answer here:
randomly select value from a JavaScript object
2 answers
Suppose you have a Javascript object with some properties to display a page with background a text and a title randomly how would you loop inside it to make it display only one item on the object each time?
const list = {
b1:{
author: 'Mozart',
title: 'lacrimosa',
text: 'Lorem ipsum dolor sit amet, consectetur adipiscing elit.',
imageURL: 'url(./assets/img/image.jpg)'
},
b2:{
author: 'Choppin',
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)'
},
}
return(
<div className="background" style={{background: 'url(./assets/img/image.jpg)'}}>
<div className="caption">
<blockquote>
<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit.</p>
<p> artist - <cite>Song</cite></p>
</blockquote>
</div>
</div>
)
I tried the Math.random() method but it didn't work
This question already has an answer here:
randomly select value from a JavaScript object
2 answers
javascript arrays reactjs loops object
javascript arrays reactjs loops object
asked Nov 16 '18 at 9:24
user3359675user3359675
184
184
marked as duplicate by user3297291, Community♦ Nov 26 '18 at 1:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by user3297291, Community♦ Nov 26 '18 at 1:00
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
1
Do you want to render random element fromlist
object ?
– t1m0n
Nov 16 '18 at 9:27
1
Have a look at this question that explains how to get a random value from an object.
– user3297291
Nov 16 '18 at 9:27
Do you have any code other than an object, like a function that usesMath.random
?
– zer00ne
Nov 16 '18 at 9:29
add a comment |
1
Do you want to render random element fromlist
object ?
– t1m0n
Nov 16 '18 at 9:27
1
Have a look at this question that explains how to get a random value from an object.
– user3297291
Nov 16 '18 at 9:27
Do you have any code other than an object, like a function that usesMath.random
?
– zer00ne
Nov 16 '18 at 9:29
1
1
Do you want to render random element from
list
object ?– t1m0n
Nov 16 '18 at 9:27
Do you want to render random element from
list
object ?– t1m0n
Nov 16 '18 at 9:27
1
1
Have a look at this question that explains how to get a random value from an object.
– user3297291
Nov 16 '18 at 9:27
Have a look at this question that explains how to get a random value from an object.
– user3297291
Nov 16 '18 at 9:27
Do you have any code other than an object, like a function that uses
Math.random
?– zer00ne
Nov 16 '18 at 9:29
Do you have any code other than an object, like a function that uses
Math.random
?– zer00ne
Nov 16 '18 at 9:29
add a comment |
6 Answers
6
active
oldest
votes
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);
The Object.getOwnPropertyNames() method would also work in your specific example.
Worked like a charm! Thank you
– user3359675
Nov 20 '18 at 2:53
add a comment |
I would better suggest (in order to prevent possible variations of object naming instead of b1, b2, etc.) to use the Object.keys() method:
var listKeys = Object.keys(list);
var randomIndex = Math.floor(Math.random() * listKeys.length);
var randomObject = list[listKeys[randomIndex]];
add a comment |
You can get random item from the object like this
const randomItem = list['b' + Math.floor(Math.random() * 2)]
add a comment |
Either convert the object to Array
so you can use Math.random()
or create a list of keys, choose one and use it to access the object.
Convert to Array
const arrList = Object.keys(list).map(k => list[k]);
const item = arrList[Math.round(Math.random() * arrList.length - 1)];
Use a list of keys
const keys = ['b1', 'b2', 'b3'];
const item = list[keys[Math.round(Math.random() * keys.length - 1)]];
add a comment |
const randomItem = function (obj) {
var keys = Object.keys(obj)
return obj[keys[ keys.length * Math.random() << 0]];
};
Are you able to provide some explanation of what's happening here?
– OliverRadini
Nov 16 '18 at 10:41
add a comment |
You can do this with 3 simple lines of code.
//find the length of your object
const length = Object.keys(list).length
//generate a random number that's between 0 and the length of your object
const random_number = Math.floor((Math.random() * length) + 0);
//get your random item from the object
const random_item = list[Object.keys(list)[random_number]]
console.log(random_item)
Keys can be completely dynamic (b1, b2, b3 can be named as anything - eg: b1, g4, aaa)
add a comment |
6 Answers
6
active
oldest
votes
6 Answers
6
active
oldest
votes
active
oldest
votes
active
oldest
votes
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);
The Object.getOwnPropertyNames() method would also work in your specific example.
Worked like a charm! Thank you
– user3359675
Nov 20 '18 at 2:53
add a comment |
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);
The Object.getOwnPropertyNames() method would also work in your specific example.
Worked like a charm! Thank you
– user3359675
Nov 20 '18 at 2:53
add a comment |
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);
The Object.getOwnPropertyNames() method would also work in your specific example.
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);
The Object.getOwnPropertyNames() method would also work in your specific example.
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);
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);
answered Nov 16 '18 at 9:59
RolvernewRolvernew
2117
2117
Worked like a charm! Thank you
– user3359675
Nov 20 '18 at 2:53
add a comment |
Worked like a charm! Thank you
– user3359675
Nov 20 '18 at 2:53
Worked like a charm! Thank you
– user3359675
Nov 20 '18 at 2:53
Worked like a charm! Thank you
– user3359675
Nov 20 '18 at 2:53
add a comment |
I would better suggest (in order to prevent possible variations of object naming instead of b1, b2, etc.) to use the Object.keys() method:
var listKeys = Object.keys(list);
var randomIndex = Math.floor(Math.random() * listKeys.length);
var randomObject = list[listKeys[randomIndex]];
add a comment |
I would better suggest (in order to prevent possible variations of object naming instead of b1, b2, etc.) to use the Object.keys() method:
var listKeys = Object.keys(list);
var randomIndex = Math.floor(Math.random() * listKeys.length);
var randomObject = list[listKeys[randomIndex]];
add a comment |
I would better suggest (in order to prevent possible variations of object naming instead of b1, b2, etc.) to use the Object.keys() method:
var listKeys = Object.keys(list);
var randomIndex = Math.floor(Math.random() * listKeys.length);
var randomObject = list[listKeys[randomIndex]];
I would better suggest (in order to prevent possible variations of object naming instead of b1, b2, etc.) to use the Object.keys() method:
var listKeys = Object.keys(list);
var randomIndex = Math.floor(Math.random() * listKeys.length);
var randomObject = list[listKeys[randomIndex]];
answered Nov 16 '18 at 9:46
Pablo AlvarezPablo Alvarez
566
566
add a comment |
add a comment |
You can get random item from the object like this
const randomItem = list['b' + Math.floor(Math.random() * 2)]
add a comment |
You can get random item from the object like this
const randomItem = list['b' + Math.floor(Math.random() * 2)]
add a comment |
You can get random item from the object like this
const randomItem = list['b' + Math.floor(Math.random() * 2)]
You can get random item from the object like this
const randomItem = list['b' + Math.floor(Math.random() * 2)]
answered Nov 16 '18 at 9:31
Nurbol AlpysbayevNurbol Alpysbayev
4,8241533
4,8241533
add a comment |
add a comment |
Either convert the object to Array
so you can use Math.random()
or create a list of keys, choose one and use it to access the object.
Convert to Array
const arrList = Object.keys(list).map(k => list[k]);
const item = arrList[Math.round(Math.random() * arrList.length - 1)];
Use a list of keys
const keys = ['b1', 'b2', 'b3'];
const item = list[keys[Math.round(Math.random() * keys.length - 1)]];
add a comment |
Either convert the object to Array
so you can use Math.random()
or create a list of keys, choose one and use it to access the object.
Convert to Array
const arrList = Object.keys(list).map(k => list[k]);
const item = arrList[Math.round(Math.random() * arrList.length - 1)];
Use a list of keys
const keys = ['b1', 'b2', 'b3'];
const item = list[keys[Math.round(Math.random() * keys.length - 1)]];
add a comment |
Either convert the object to Array
so you can use Math.random()
or create a list of keys, choose one and use it to access the object.
Convert to Array
const arrList = Object.keys(list).map(k => list[k]);
const item = arrList[Math.round(Math.random() * arrList.length - 1)];
Use a list of keys
const keys = ['b1', 'b2', 'b3'];
const item = list[keys[Math.round(Math.random() * keys.length - 1)]];
Either convert the object to Array
so you can use Math.random()
or create a list of keys, choose one and use it to access the object.
Convert to Array
const arrList = Object.keys(list).map(k => list[k]);
const item = arrList[Math.round(Math.random() * arrList.length - 1)];
Use a list of keys
const keys = ['b1', 'b2', 'b3'];
const item = list[keys[Math.round(Math.random() * keys.length - 1)]];
answered Nov 16 '18 at 9:33
DavidDavid
1,163118
1,163118
add a comment |
add a comment |
const randomItem = function (obj) {
var keys = Object.keys(obj)
return obj[keys[ keys.length * Math.random() << 0]];
};
Are you able to provide some explanation of what's happening here?
– OliverRadini
Nov 16 '18 at 10:41
add a comment |
const randomItem = function (obj) {
var keys = Object.keys(obj)
return obj[keys[ keys.length * Math.random() << 0]];
};
Are you able to provide some explanation of what's happening here?
– OliverRadini
Nov 16 '18 at 10:41
add a comment |
const randomItem = function (obj) {
var keys = Object.keys(obj)
return obj[keys[ keys.length * Math.random() << 0]];
};
const randomItem = function (obj) {
var keys = Object.keys(obj)
return obj[keys[ keys.length * Math.random() << 0]];
};
answered Nov 16 '18 at 9:54
Maher TlibaMaher Tliba
346211
346211
Are you able to provide some explanation of what's happening here?
– OliverRadini
Nov 16 '18 at 10:41
add a comment |
Are you able to provide some explanation of what's happening here?
– OliverRadini
Nov 16 '18 at 10:41
Are you able to provide some explanation of what's happening here?
– OliverRadini
Nov 16 '18 at 10:41
Are you able to provide some explanation of what's happening here?
– OliverRadini
Nov 16 '18 at 10:41
add a comment |
You can do this with 3 simple lines of code.
//find the length of your object
const length = Object.keys(list).length
//generate a random number that's between 0 and the length of your object
const random_number = Math.floor((Math.random() * length) + 0);
//get your random item from the object
const random_item = list[Object.keys(list)[random_number]]
console.log(random_item)
Keys can be completely dynamic (b1, b2, b3 can be named as anything - eg: b1, g4, aaa)
add a comment |
You can do this with 3 simple lines of code.
//find the length of your object
const length = Object.keys(list).length
//generate a random number that's between 0 and the length of your object
const random_number = Math.floor((Math.random() * length) + 0);
//get your random item from the object
const random_item = list[Object.keys(list)[random_number]]
console.log(random_item)
Keys can be completely dynamic (b1, b2, b3 can be named as anything - eg: b1, g4, aaa)
add a comment |
You can do this with 3 simple lines of code.
//find the length of your object
const length = Object.keys(list).length
//generate a random number that's between 0 and the length of your object
const random_number = Math.floor((Math.random() * length) + 0);
//get your random item from the object
const random_item = list[Object.keys(list)[random_number]]
console.log(random_item)
Keys can be completely dynamic (b1, b2, b3 can be named as anything - eg: b1, g4, aaa)
You can do this with 3 simple lines of code.
//find the length of your object
const length = Object.keys(list).length
//generate a random number that's between 0 and the length of your object
const random_number = Math.floor((Math.random() * length) + 0);
//get your random item from the object
const random_item = list[Object.keys(list)[random_number]]
console.log(random_item)
Keys can be completely dynamic (b1, b2, b3 can be named as anything - eg: b1, g4, aaa)
answered Nov 16 '18 at 11:09
SheharaShehara
1013
1013
add a comment |
add a comment |
1
Do you want to render random element from
list
object ?– t1m0n
Nov 16 '18 at 9:27
1
Have a look at this question that explains how to get a random value from an object.
– user3297291
Nov 16 '18 at 9:27
Do you have any code other than an object, like a function that uses
Math.random
?– zer00ne
Nov 16 '18 at 9:29