Loop through object and get random item in javascript [duplicate]












1
















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










share|improve this 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 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
















1
















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










share|improve this 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 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














1












1








1









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










share|improve this question















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






share|improve this question













share|improve this question











share|improve this question




share|improve this question










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 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














  • 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








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












6 Answers
6






active

oldest

votes


















0














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.






share|improve this answer
























  • Worked like a charm! Thank you

    – user3359675
    Nov 20 '18 at 2:53



















1














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]];





share|improve this answer































    0














    You can get random item from the object like this



    const randomItem = list['b' + Math.floor(Math.random() * 2)]





    share|improve this answer































      0














      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)]];





      share|improve this answer































        0














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





        share|improve this answer
























        • Are you able to provide some explanation of what's happening here?

          – OliverRadini
          Nov 16 '18 at 10:41



















        0














        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)






        share|improve this answer






























          6 Answers
          6






          active

          oldest

          votes








          6 Answers
          6






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          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.






          share|improve this answer
























          • Worked like a charm! Thank you

            – user3359675
            Nov 20 '18 at 2:53
















          0














          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.






          share|improve this answer
























          • Worked like a charm! Thank you

            – user3359675
            Nov 20 '18 at 2:53














          0












          0








          0







          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.






          share|improve this answer













          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);






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 16 '18 at 9:59









          RolvernewRolvernew

          2117




          2117













          • 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





          Worked like a charm! Thank you

          – user3359675
          Nov 20 '18 at 2:53













          1














          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]];





          share|improve this answer




























            1














            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]];





            share|improve this answer


























              1












              1








              1







              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]];





              share|improve this answer













              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]];






              share|improve this answer












              share|improve this answer



              share|improve this answer










              answered Nov 16 '18 at 9:46









              Pablo AlvarezPablo Alvarez

              566




              566























                  0














                  You can get random item from the object like this



                  const randomItem = list['b' + Math.floor(Math.random() * 2)]





                  share|improve this answer




























                    0














                    You can get random item from the object like this



                    const randomItem = list['b' + Math.floor(Math.random() * 2)]





                    share|improve this answer


























                      0












                      0








                      0







                      You can get random item from the object like this



                      const randomItem = list['b' + Math.floor(Math.random() * 2)]





                      share|improve this answer













                      You can get random item from the object like this



                      const randomItem = list['b' + Math.floor(Math.random() * 2)]






                      share|improve this answer












                      share|improve this answer



                      share|improve this answer










                      answered Nov 16 '18 at 9:31









                      Nurbol AlpysbayevNurbol Alpysbayev

                      4,8241533




                      4,8241533























                          0














                          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)]];





                          share|improve this answer




























                            0














                            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)]];





                            share|improve this answer


























                              0












                              0








                              0







                              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)]];





                              share|improve this answer













                              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)]];






                              share|improve this answer












                              share|improve this answer



                              share|improve this answer










                              answered Nov 16 '18 at 9:33









                              DavidDavid

                              1,163118




                              1,163118























                                  0














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





                                  share|improve this answer
























                                  • Are you able to provide some explanation of what's happening here?

                                    – OliverRadini
                                    Nov 16 '18 at 10:41
















                                  0














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





                                  share|improve this answer
























                                  • Are you able to provide some explanation of what's happening here?

                                    – OliverRadini
                                    Nov 16 '18 at 10:41














                                  0












                                  0








                                  0







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





                                  share|improve this answer













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






                                  share|improve this answer












                                  share|improve this answer



                                  share|improve this answer










                                  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



















                                  • 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











                                  0














                                  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)






                                  share|improve this answer




























                                    0














                                    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)






                                    share|improve this answer


























                                      0












                                      0








                                      0







                                      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)






                                      share|improve this answer













                                      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)







                                      share|improve this answer












                                      share|improve this answer



                                      share|improve this answer










                                      answered Nov 16 '18 at 11:09









                                      SheharaShehara

                                      1013




                                      1013















                                          Popular posts from this blog

                                          Florida Star v. B. J. F.

                                          Danny Elfman

                                          Retrieve a Users Dashboard in Tumblr with R and TumblR. Oauth Issues