从Javascript对象中选择随机属性和值

时间:2017-03-30 10:49:30

标签: javascript html

我刚刚开始学习Javascript并需要一些帮助。

我想从我完成的数组中构建一个随机选择器

var movie = ["star wars", "lotr", "moonlight", "avengers"]

function newMovie() {
  var randomNumber = Math.floor(Math.random() * (movie.length));

  document.getElementById("display").innerHTML = "You should watch " + movie[randomNumber];
}

现在我想为书籍,游戏等添加更多选项..所以我为它创建了一个对象:

var chooseOption = {
    watch: ["star wars", "lotr", "moonlight", "avengers"],
    read: ["scott pilgrim", "harry potter", "eragon"],
    play: ["starbound", "skyrim", "bioshock", "fallout"]
}

我对如何继续有点迷失 - 我希望它首先挑选出一个属性,然后选择所述属性的一个值,所有这些都是随机的。 然后在"You should" + [property] + [value]

中打印出来

我该怎么做?对象是一种可行的方法,还是更好的选择?

4 个答案:

答案 0 :(得分:3)

因此,首先编写一个带有数组并返回项的泛型选择函数。我将把这个实现留给你。

接下来,您需要使用Object.keys来获取类似['watch', 'read', 'play']的数组。

var chooseOption = {
    watch: ["star wars", "lotr", "moonlight", "avengers"],
    read: ["scott pilgrim", "harry potter", "eragon"],
    play: ["starbound", "skyrim", "bioshock", "fallout"]
}
var keys = Object.keys(chooseOption);
var type = pick(keys);

然后你可以通过该键访问属性来获取内部数组。

var items = chooseOption[type];
var item = pick(items);

最后,显示结果。

console.log('You should ' + type + ' ' + item);

答案 1 :(得分:0)

以下是使用Math.random()实现随机性的另一种解决方案:

var chooseOption = {
    watch: ["star wars", "lotr", "moonlight", "avengers"],
    read: ["scott pilgrim", "harry potter", "eragon"],
    play: ["starbound", "skyrim", "bioshock", "fallout"]
}


function getRandom(obj, item){
  // 1) select which property to operate on.
  var itemArr = obj[item];

  // 2) we already know that the key can only be an integer(between 0 and length -1 )

  // Math.random()   : to generate a random number(between [0-1]).
  // Math.round()    : to round a number to an integer.
  // number % length : to make sure the result would be an integer between 0 and length -1 
  var index = Math.round((Math.random() * itemArr.length)) % itemArr.length;


  return {
    property:itemArr,
    index: index,
    value: itemArr[index],
  }

}


console.log(getRandom(chooseOption,'watch'));
console.log(getRandom(chooseOption,'read'));
console.log(getRandom(chooseOption,'play'));

答案 2 :(得分:0)

object.keys() Math.random()

一起使用
  
      
  1. 首先使用Object.keys().length从对象中选择随机密钥。
  2.   
  3. 然后获得密钥的受尊重数组。
  4.   
  5. 然后使用数组长度
  6. 从数组中选择随机值   

function newMovie() {
var types =Object.keys(chooseOption);
  var rn = Math.floor(Math.random() * (types.length));
var t = types[rn];
var val = chooseOption[types[rn]];
var vn = Math.floor(Math.random() * (val.length));
var v = val[vn]
 console.log("You should watch " +t +' '+v);
}

var chooseOption = {
  watch: ["star wars", "lotr", "moonlight", "avengers"],
  read: ["scott pilgrim", "harry potter", "eragon"],
  play: ["starbound", "skyrim", "bioshock", "fallout"]
}
newMovie()

答案 3 :(得分:0)

你走了,

    var options = {
        watch: ["star wars", "lotr", "moonlight", "avengers"],
        read: ["scott pilgrim", "harry potter", "eragon"],
        play: ["starbound", "skyrim", "bioshock", "fallout"]
    };

    for( i in options){
        //i is will be iterating with values "watch","read","play"
        t = Math.floor( Math.random()* options[i].length);
        //options[i] is to access the value of property eg: options["watch"]
        //will return ["star wars", "lotr", "moonlight", "avengers"]
        console.log("You should " + i +" "+ options[i][t]);
    }

而是使用代码登录其他内容:)