提问者:小点点

从具有子[重复]的常量中获取随机元素


null

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

null

这是密码。 由于某种原因,我不能使用example1[math.flood(math.random()*example1.length)]代码。 我如何从子常量中获得一个随机项(如果它们不是子常量,实际上idk它们的名称)。


共1个答案

匿名用户

你可以做这样的事。

null

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

console.log(Object.keys(example1)[Math.floor(Math.random()*Object.keys(example1).length)]);