0

I've been searching for how to get to an object randomly, but I need to get another random object that is inside the first random.

This is a card game I want to try.

To be clear my objects are 4 in which each one they have 10 other objects. I need to get first a random one and with that object get the randoms inside. It needs to be the ones on the selected one because each of them have different values.

I have this

var palos= {
    espada: {
        uno:70, dos:45, tres:50, cuatro:5, cinco:10, seis:15, siete:60, diez:25, once:30, doce:35
    },
    basto: {
        uno:65, dos:45, tres:50, cuatro:5, cinco:10, seis:15, siete:20, diez:25, once:30, doce:35
    },
    Oro: {
        uno:40, dos:45, tres:50, cuatro:5, cinco:10, seis:15, siete:55, diez:25, once:30, doce:35
    },
    copa: {
        uno:40, dos:45, tres:50, cuatro:5, cinco:10, seis:15, siete:20, diez:25, once:30, doce:35
    },
    
}

var palosdados = Object.keys(palos)[Math.floor(Math.random()*Object.keys(palos).length)];
console.log(palosdados);
Jason Aller
  • 3,541
  • 28
  • 38
  • 38
atallpa
  • 9
  • 1

1 Answers1

0

Use this funtion

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

let ParentRandom = randomProperty(palos);
let ChildRandom = randomProperty(ParentRandom)
console.log(ParentRandom, ChildRandom)
Sanoodia
  • 830
  • 4
  • 9
  • 1
    credit: https://stackoverflow.com/questions/2532218/pick-random-property-from-a-javascript-object – thedude Aug 23 '21 at 07:50
  • Thats just gives me whole list of objects. I need only one from the list that belongs to the randomly chosen. – atallpa Aug 24 '21 at 09:06