mardi 24 octobre 2017

How to select an random index from randomly selected object?

I want to select a random index from an array object, this object contains two keys author and images array.. I can select a random index from array object but how would i also select a random index from images array based on the random selected array object index?

    let resourcesObject = [
      {
        "author" : "photographer1",
        "images1": ['cat1','cat2','cat3'] 

      },
      {
        "author" : "photographer2",
        "images2": ['dog1', 'dog2', 'dog3', 'dog4'] 

      }

      ];

    console.log(resourcesObject[Math.floor(Math.random() * resourcesObject.length)]); // gives me a random object as expected { author: 'photographer2',images2: [ 'dog1', 'dog2', 'dog3', 'dog4' ] }, //but i also want to select a random index from the images2 how would i do that?




Aucun commentaire:

Enregistrer un commentaire