javascript Javascript变量中的多个值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15817932/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 02:12:29  来源:igfitidea点击:

Javascript multiple values in variable

javascript

提问by user1826795

Is there any way to have one variable with multiple values like this:

有没有办法让一个变量具有多个值,如下所示:

var variable = 1, 2, 3;
var enteredVal = 1;

if (enteredVal == variable){
    alert('You chose the right number');
}

So if the variable enteredVal is equal to 1, 2 or 3, it will alert the message. I can't seem to get my head around it.

因此,如果变量 enterVal 等于 1、2 或 3,它将警告消息。我似乎无法理解它。

回答by ajp15243

There is no way to assign multiple distinct values to a single variable.

无法将多个不同的值分配给单个变量。

An alternative is to have variablebe an Array, and you can check to see if enteredvalis in the array.

另一种方法是拥有variable一个Array,您可以检查它是否enteredval在数组中。

var variable = [1, 2, 3];
var enteredval = 1;

if (variable.indexOf(enteredval) > -1){
    alert('you chose the right number');
}

Note that indexOfon an array is not usable in IE8 and below(see the Requirements section at the bottom). In that case you would need to use a framework/library's method, or write it yourself:

请注意,indexOf数组在 IE8 及以下版本中不可用(请参阅底部的“要求”部分)。在这种情况下,您需要使用框架/库的方法,或者自己编写:

var variable = [1, 2, 3];
var enteredval = 1;

for (var i = 0; i < variable.length; i++) {
    if (variable[i] === enteredval) {
        alert('you chose the right number');
        break; // No need to check all the other values in variable
    }
}

To modify arrays after you have instantiated them, take a look at push, pop, shift, and unshiftfor adding/removing values. For modifying existing values, you can directly access the index and reassign the value.

要修改阵列已实例化后,他们,看看pushpopshift,和unshift添加/删除值。要修改现有值,您可以直接访问索引并重新分配值。

variable[1] = 5;
// variable is now [1, 5, 3] since arrays are 0-indexed

回答by Sean Vieira

You can store them in an array and use indexOf(as long as you don't need IE 8 support):

您可以将它们存储在一个数组中并使用indexOf(只要您不需要 IE 8 支持):

var supportedOptions = [1, 2, 3],
    enteredVal = 1;

if(supportedOptions.indexOf(enteredVal) !== -1) alert("Yep!");

回答by Bucket

Try using an array:

尝试使用数组:

var variables = [1, 2, 3];

You can access members of the array with variables[i], where iis the index of the array, starting at 0.

您可以使用 访问数组成员variables[i],其中i是数组的索引,从 开始0

variables[0]returns 1

variables[0]回报 1

variables[1]returns 2

variables[1]回报 2

variables[2]returns 3

variables[2]回报 3

This way you can use a forloop to iterate through the array:

这样您就可以使用for循环来遍历数组:

var enteredval = 1; //your guess
for(var i = 0; i < variables.length; i++) { //go through each element
    if(variables[i] == enteredval) { //if they match...
        alert("You chose the right number!"); //...then you got it!
    }
}

回答by PhiAgent

You can store values of the variable as keys in an object. eg:

您可以将变量的值存储为对象中的键。例如:

let variable={
  1:0,
  2:0,
  3:0
}

enteredVal=3

if (enteredVal in variable) {
  alert('You chose the right number');
}

回答by Filling The Stack is What I DO

You can implement the indexof function of a string.

可以实现字符串的 indexof 函数。

var variable = '1,2,3';
    var enteredval = '1';

    if (variable.indexOf(enteredval) > -1) { alert('you choosed the right number'); };

回答by What have you tried

You are going to want to create an arraywhich can hold multiple values. You would then want to use index ofto determine if the value is found in the array.

您将要创建一个array可以容纳多个值的对象。然后,您可能希望使用它index of来确定是否在数组中找到了该值。

var myArray = new Array();

myArray[0] = 1;
myArray[1] = 2;
myArray[2] = 3;

Check the value:

检查值:

if(myArray.indexOf(1) > -1){
// exists
}