【问题标题】:Randomize elements within objects within arrays for javascript随机化javascript数组中对象内的元素
【发布时间】:2019-01-31 05:34:13
【问题描述】:

我想创建一个简短有趣的项目,我可以在其中随机化“答案”函数,并且可以选择显示该随机对象的“谜语”元素。有没有办法做到这一点?

var answers = {

  reflection:{answer:"reflection", riddle:"You saw me where I could not be. Yet, often you see me. What am I?", level:"medium"},
  rainbow:{answer:"rainbow", riddle:"I am beautiful, up in the sky. I am magical, yet I cannot fly. To people I bring luck, to some people, riches. The boy at my end does whatever he wishes. What am I?", level:"easy"},
  comet: {answer:"comet", riddle:"What zips through the sky with a tail of fire and dust. It could be an omen, its origin to discuss?", level:"easy"},
  spine: {answer:"spine", riddle:"This is needed both for courage and hardcover books.", level:"medium"},
  eyes: {answer:"eyes", riddle:"We are two brothers on opposite sides of the road, but we never see each other. Who are we?", level:"medium"},
  sting: {answer:"sting", riddle:"If you're stealing honey, be prepared to receive vengeance in this form", level:"easy"},
  push: {answer:"push", riddle:"It can be done to buttons and shopping carts. What is it?", level:"easy"},
  wine: {answer:"wine", riddle:"The older this thing grows the more valued it becomes. It is always much better when its breathing is done.", level:"medium"},
  laundry: {answer:"laundry", riddle:"When it's dirty this should never be aired in public.", level:"easy"},
  pillow: {answer:"pillow", riddle:"I lose my head in the morning and regain back it at night. What am I?", level:"medium"},
  tennis: {answer:"tennis", riddle:"A sport with love and service played by singles and pairs.", level:"easy"},
  bell: {answer:"bell", riddle:"It is able to speak because it has a hard gone. You know what it is as soon as it has sung. What is it?", level:"medium"},
  wheelbarrow: {answer:"wheelbarrow", riddle:"I have two legs, but they only touch the ground while I'm at rest. What am I?", level:"Hard"},
};

// Randomize the riddles

var random = answers[Math.floor(Math.random() * answers.length)];
// document.getElementById('riddle').innerHTML = random;

console.log(random);

【问题讨论】:

  • 问题是什么?
  • .length 用于数组。 answers 是一个对象,而不是一个数组。
  • 对不起,一团糟,我正在测试如何发布代码并使其背后有灰色背景。第一次在stackoverflow上发帖

标签: javascript html arrays object


【解决方案1】:

answers 是一个对象,但您使用的代码是用于选择数组的随机元素。

您可以使用Object.keys() 获取所有对象属性名称的数组,然后从中选择一个随机元素。

var answers = {

  reflection:{answer:"reflection", riddle:"You saw me where I could not be. Yet, often you see me. What am I?", level:"medium"},
  rainbow:{answer:"rainbow", riddle:"I am beautiful, up in the sky. I am magical, yet I cannot fly. To people I bring luck, to some people, riches. The boy at my end does whatever he wishes. What am I?", level:"easy"},
  comet: {answer:"comet", riddle:"What zips through the sky with a tail of fire and dust. It could be an omen, its origin to discuss?", level:"easy"},
  spine: {answer:"spine", riddle:"This is needed both for courage and hardcover books.", level:"medium"},
  eyes: {answer:"eyes", riddle:"We are two brothers on opposite sides of the road, but we never see each other. Who are we?", level:"medium"},
  sting: {answer:"sting", riddle:"If you're stealing honey, be prepared to receive vengeance in this form", level:"easy"},
  push: {answer:"push", riddle:"It can be done to buttons and shopping carts. What is it?", level:"easy"},
  wine: {answer:"wine", riddle:"The older this thing grows the more valued it becomes. It is always much better when its breathing is done.", level:"medium"},
  laundry: {answer:"laundry", riddle:"When it's dirty this should never be aired in public.", level:"easy"},
  pillow: {answer:"pillow", riddle:"I lose my head in the morning and regain back it at night. What am I?", level:"medium"},
  tennis: {answer:"tennis", riddle:"A sport with love and service played by singles and pairs.", level:"easy"},
  bell: {answer:"bell", riddle:"It is able to speak because it has a hard gone. You know what it is as soon as it has sung. What is it?", level:"medium"},
  wheelbarrow: {answer:"wheelbarrow", riddle:"I have two legs, but they only touch the ground while I'm at rest. What am I?", level:"Hard"},
};

var keys = Object.keys(answers);
var randomKey = keys[Math.floor(Math.random() * keys.length)];
var random = answers[randomKey];

console.log(random);

【讨论】:

  • 您先生,真是个天才!您可能认为这很容易,但是对于刚刚开始学习 Javascript 的我来说,这对我来说是一个很大的突破。谢谢。
猜你喜欢
  • 2010-10-23
  • 2021-10-23
  • 2011-04-18
  • 1970-01-01
  • 2011-04-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多