I have an array and simply wants to get the element / value at index 1
我有一个数组,只想在索引1处获得元素/值
var myValues = new Array();
var valueAtIndex1 = myValues.getValue(1); // (something like this)
How can I get the value at the 1st index of my array in JavaScript?
如何在JavaScript中获取数组的第一个索引值?
4 个解决方案
#1
102
Just use indexer
只使用索引器
var valueAtIndex1 = myValues[1];
#2
28
Array indexes in JavaScript start at zero for the first item, so try this:
对于第一个条目,JavaScript中的数组索引从0开始,请尝试以下操作:
var firstArrayItem = myValues[0]
Of course, if you actually want the second item in the array at index 1, then it's myValues[1]
.
当然,如果你想要数组中的第二项在索引1处,那么它就是myValues[1]。
See Accessing array elements for more info.
有关更多信息,请参见访问数组元素。
#3
7
You can just use []
:
你可以用[]:
var valueAtIndex1 = myValues[1];
#4
-3
I know this is kind of late but here's another way :)
我知道这有点晚了,但还有另一种方法:
// var psn_ = myForm.elements['array_name[]']; // --> getting array by name
var myValues = new Array();
if( myValues.length == undefined ) { // if length is 1, length becomes undefined
myValues = [].concat(myValues); // make 'myValues' an array
var tempVar = myValues[0].value; // get and store value
}
else{
var tempVar = ""; // temporary variable to store all values of array
for (var i = 0; i < myValues.length; i++) {
tempVar += myValues[i].value; // example ["one", "two"]
tempVar += ','; // tempVar = one,two,
}
tempVar = tempVar.substring(',', tempVar.length - 1); // tempVar = one,two
}
Passing to another file
传递到另一个文件
... "url_name.php?tempVar=" + tempVar + ... // passing through URL
Get the values in PHP through
通过PHP获取值
$getValue = $_GET['tempVar']; // get value from tempVar in URL
$getValue_Array = explode(',', $getValue); // getValue_Array[0] = "one"
// getValue_Array[1] = "two"
#1
102
Just use indexer
只使用索引器
var valueAtIndex1 = myValues[1];
#2
28
Array indexes in JavaScript start at zero for the first item, so try this:
对于第一个条目,JavaScript中的数组索引从0开始,请尝试以下操作:
var firstArrayItem = myValues[0]
Of course, if you actually want the second item in the array at index 1, then it's myValues[1]
.
当然,如果你想要数组中的第二项在索引1处,那么它就是myValues[1]。
See Accessing array elements for more info.
有关更多信息,请参见访问数组元素。
#3
7
You can just use []
:
你可以用[]:
var valueAtIndex1 = myValues[1];
#4
-3
I know this is kind of late but here's another way :)
我知道这有点晚了,但还有另一种方法:
// var psn_ = myForm.elements['array_name[]']; // --> getting array by name
var myValues = new Array();
if( myValues.length == undefined ) { // if length is 1, length becomes undefined
myValues = [].concat(myValues); // make 'myValues' an array
var tempVar = myValues[0].value; // get and store value
}
else{
var tempVar = ""; // temporary variable to store all values of array
for (var i = 0; i < myValues.length; i++) {
tempVar += myValues[i].value; // example ["one", "two"]
tempVar += ','; // tempVar = one,two,
}
tempVar = tempVar.substring(',', tempVar.length - 1); // tempVar = one,two
}
Passing to another file
传递到另一个文件
... "url_name.php?tempVar=" + tempVar + ... // passing through URL
Get the values in PHP through
通过PHP获取值
$getValue = $_GET['tempVar']; // get value from tempVar in URL
$getValue_Array = explode(',', $getValue); // getValue_Array[0] = "one"
// getValue_Array[1] = "two"