-1

const example1 = {
  subex1: {
    ...something...
  },
  subex2: {
    ...something...
  }
};

This is the code. For some reason, I can't use example1[Math.floor(Math.random() * example1.length)] code. How can I get a random item from sub constants (actually idk their name if they're not subconst).

1 Answers1

1

You can do something like this.

const example1 = {
  subex1: {
    "hello": "hello"
  },
  subex2: {
    "Hello1": "hello1"
  }
};

console.log(Object.keys(example1)[Math.floor(Math.random()*Object.keys(example1).length)]);
xMayank
  • 1,875
  • 2
  • 5
  • 19