访问javascript html列表中的元素

时间:2022-05-14 15:55:05

I have a list - [getFamilyPkg, getActivePkg, getEligiblePkg, getActivePkgForDeactivation], want to get each element in this list in a javascript function.
How to do it? Thanks in advance.

我有一个列表 - [getFamilyPkg,getActivePkg,getEligiblePkg,getActivePkgForDeactivation],想要在javascript函数中获取此列表中的每个元素。怎么做?提前致谢。

2 个解决方案

#1


4  

Assume list is stored in a variable called arr.

假设列表存储在名为arr的变量中。

for (var i = 0; i < arr.length; i++) {
  var elem = arr[i];
  // elem is either getFamilyPkg, or getActivePkg, or ...
}

#2


1  

Tommy's answer is actually incorrect. for (var i in arr) loops over all attributes/properties of the arr object prototype, which includes builtin functions like slice(), etc. See jsfiddle example here: http://jsfiddle.net/sEtMw/ (check the console and you will see a bunch of extra properties other than the array elements.

汤米的回答实际上是错误的。 for(var i in arr)循环遍历arr对象原型的所有属性/属性,其中包括像slice()等内置函数。请参阅jsfiddle示例:http://jsfiddle.net/sEtMw/(检查控制台和你会看到除了数组元素之外的一堆额外属性。

There are a few approaches (once again assuming the array is named arr):

有几种方法(再次假设数组名为arr):

var elem;
for (var i = 0, len = arr.length; i < len; i++) {
    elem = arr[i];
    // elem is actually one of the array elements here
}

Another option is as follows:

另一种选择如下:

var elem;
while (elem = arr.shift()) {
    // elem is one of the array elements
}

Please note that the second approach does not keep the array intact. i.e. at the end of the loop, the array will be empty. But if you are okay with that, the syntax is definitely cleaner. Both approaches are illustrated here: http://jsfiddle.net/z7zKK/. The variable arr is logged at the end to illustrate that it is empty.

请注意,第二种方法不能保持阵列完整。即在循环结束时,数组将为空。但如果你对它没问题,语法肯定更清晰。这两种方法都在这里说明:http://jsfiddle.net/z7zKK/。最后记录变量arr以说明它是空的。

#1


4  

Assume list is stored in a variable called arr.

假设列表存储在名为arr的变量中。

for (var i = 0; i < arr.length; i++) {
  var elem = arr[i];
  // elem is either getFamilyPkg, or getActivePkg, or ...
}

#2


1  

Tommy's answer is actually incorrect. for (var i in arr) loops over all attributes/properties of the arr object prototype, which includes builtin functions like slice(), etc. See jsfiddle example here: http://jsfiddle.net/sEtMw/ (check the console and you will see a bunch of extra properties other than the array elements.

汤米的回答实际上是错误的。 for(var i in arr)循环遍历arr对象原型的所有属性/属性,其中包括像slice()等内置函数。请参阅jsfiddle示例:http://jsfiddle.net/sEtMw/(检查控制台和你会看到除了数组元素之外的一堆额外属性。

There are a few approaches (once again assuming the array is named arr):

有几种方法(再次假设数组名为arr):

var elem;
for (var i = 0, len = arr.length; i < len; i++) {
    elem = arr[i];
    // elem is actually one of the array elements here
}

Another option is as follows:

另一种选择如下:

var elem;
while (elem = arr.shift()) {
    // elem is one of the array elements
}

Please note that the second approach does not keep the array intact. i.e. at the end of the loop, the array will be empty. But if you are okay with that, the syntax is definitely cleaner. Both approaches are illustrated here: http://jsfiddle.net/z7zKK/. The variable arr is logged at the end to illustrate that it is empty.

请注意,第二种方法不能保持阵列完整。即在循环结束时,数组将为空。但如果你对它没问题,语法肯定更清晰。这两种方法都在这里说明:http://jsfiddle.net/z7zKK/。最后记录变量arr以说明它是空的。