简体   繁体   English

使用Javascript和jQuery选择要使用变量名进行搜索的数组/对象

[英]Choosing an array/object to search from using variable name, with Javascript and jQuery

I'm a total beginner with JavaScript. 我是JavaScript的初学者。 I have code that randomly picks an item from an object and now I'd like to randomly pick an item from an object that shares the name as whatever was picked in the first round of generation. 我有一些代码可以从一个对象中随机选择一个项目,现在我想从一个对象中随机选择一个项目,该项目的名称与第一代中选择的名称相同。 I'm not sure how to use a variable name in a selector, especially if that variable name is itself, uh, variable! 我不确定如何在选择器中使用变量名,特别是如果该变量名本身,呃,变量!

Right now I'm getting "undefined" for pizzaFirstChoice.option. 现在,我对pizzaFirstChoice.option感到“未定义”。 I'd like to be getting either "extra sausage", "extra pepperoni", etc, as .option if "meatlover" is picked from "pizzaStyles" and either "extra mushroom", "extra onion", etc, if "veggielover" is picked from "pizzaStyles". 如果要从“ pizzaStyles”中选择“ meatlover”,如果要是“ veggielover”,则要使用“额外的蘑菇”,“额外的洋葱”等,我希望得到“额外的香肠”,“额外的意大利辣香肠”等。选自“ pizzaStyles”。 The code is successfully generating and displaying .size and .style. 该代码已成功生成并显示.size和.style。

I feel like I'm doing an awful job at describing this, but hopefully you can see what I'm after by looking at the code. 我觉得我在描述这一点时做得很糟糕,但是希望您可以通过看一下代码来了解我的想法。

function generate() {

var pizzaFirstChoice = {
size: "",
style: "",
option: "",
};

var pizzaSizes = [
"small",
"medium",
"large",
];

var pizzaStyles = [
"meatlover",
"veggielover",
];

var meatlover = [
"extra sausage",
"extra pepperoni",
"extra bacon",
];

var veggielover = [
"extra mushroom",
"extra onion",
"extra bell pepper",
];

pizzaFirstChoice.size = pizzaSizes[Math.floor(Math.random()*pizzaSizes.length)];
pizzaFirstChoice.style = pizzaStyles[Math.floor(Math.random()*pizzaStyles.length)];
pizzaFirstChoice.option = pizzaFirstChoice.style[Math.floor(Math.random()*pizzaFirstChoice.style)];

var pizza = $("#result").html("His first choice of pizza is a " + pizzaFirstChoice.size + " " + pizzaFirstChoice.style + " with " + pizzaFirstChoice.option + ".");

}

You can define meatlover and veggielover as properties of an object; 您可以将meatloverveggielover定义为对象的属性。 use bracket notation to reference pizzaFirstChoice.style as property of object with [Math.floor(Math.random() * pizzaOptions[pizzaFirstChoice.style].length)] to adjacent right to reference .length of the selected array. 使用括号表示法将pizzaFirstChoice.style作为对象的属性来引用,并具有[Math.floor(Math.random() * pizzaOptions[pizzaFirstChoice.style].length)]到相邻权利,以引用选定数组的.length

 function generate() { var pizzaFirstChoice = { size: "", style: "", option: "", }; var pizzaSizes = [ "small", "medium", "large", ]; var pizzaStyles = [ "meatlover", "veggielover", ]; // define `meatlover`, `veggielover` arrays as properties of an object var pizzaOptions = { meatlover: [ "extra sausage", "extra pepperoni", "extra bacon", ] , veggielover: [ "extra mushroom", "extra onion", "extra bell pepper", ] }; pizzaFirstChoice.size = pizzaSizes[Math.floor(Math.random() * pizzaSizes.length)]; pizzaFirstChoice.style = pizzaStyles[Math.floor(Math.random() * pizzaStyles.length)]; pizzaFirstChoice.option = pizzaOptions[pizzaFirstChoice.style][Math.floor(Math.random() * pizzaOptions[pizzaFirstChoice.style].length)]; var pizza = $("#result").html("His first choice of pizza is a " + pizzaFirstChoice.size + " " + pizzaFirstChoice.style + " with " + pizzaFirstChoice.option + "."); } generate(); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div id="result"></div> 

if you input console.log after each pizzaFirstChoice.. you will see tha first and second wil give you an answer: medium veggielover, and the third one will be undefined. 如果您在每个pizzaFirstChoice ..之后输入console.log,则会看到第一个和第二个会给您答案:中等蔬菜,而第三个则是未定义的。 However you are multiplying string pizzaFirstChoice.style with Math.random() - of course it is undefined. 但是,您正在将字符串pizzaFirstChoice.style与Math.random()相乘-当然它是未定义的。 the question is what do you want to be pizzaFirstChoice.option - in the same way you can query array length for meatlover and for veggie lover: pizzaFirstChoice.option = meatlover[Math.floor(Math.random()*meatlover.length)]; 问题是您想成为PizzaFirstChoice.option的方式是什么-用同样的方式,您可以查询Meatlover和素食者的数组长度:pizzaFirstChoice.option = meatlover [Math.floor(Math.random()* meatlover.length)] ; or pizzaFirstChoice.option = veggielover[Math.floor(Math.random()*veggielover.length)]; 或pizzaFirstChoice.option = veggielover [Math.floor(Math.random()* veggielover.length)];

I think that i get correctly what you are after. 我认为我正确地理解了你的追求。 if not - please explain what is the desired output 如果不是,请说明所需的输出是什么

Try a conditional: 尝试一个条件:

function generate() {

var pizzaFirstChoice = {
size: "",
style: "",
option: "",
};

var pizzaSizes = [
"small",
"medium",
"large",
];

var pizzaStyles = [
"meatlover",
"veggielover",
];

var meatlover = [
"extra sausage",
"extra pepperoni",
"extra bacon",
];

var veggielover = [
"extra mushroom",
"extra onion",
"extra bell pepper",
];

pizzaFirstChoice.size = pizzaSizes[Math.floor(Math.random()*pizzaSizes.length)];
pizzaFirstChoice.style = pizzaStyles[Math.floor(Math.random()*pizzaStyles.length)];

if (pizzaFirstChoice.style == 'meatlover') {
    pizzaFirstChoice.option = meatlover[Math.floor(Math.random()*meatlover.length)];
} else if (pizzaFirstChoice.style == 'veggielover') {
    pizzaFirstChoice.option = veggielover[Math.floor(Math.random()*veggielover.length)];
}

$("#result").html("His first choice of pizza is a " + pizzaFirstChoice.size + " " + pizzaFirstChoice.style + " with " + pizzaFirstChoice.option + ".");
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM