如何在JavaScript中迭代python列表(appended List)[duplicate]

oxf4rvwz  于 2022-12-17  发布在  Java
关注(0)|答案(3)|浏览(82)

此问题在此处已有答案

Loop over an array in JavaScript(40个答案)
12小时前关门了。
我有一个值,它基本上是一组附加的python列表。我如何在JS中迭代它们?
我的值如下所示:

value =  [['A', ' PARIKH', "None", "None"], 
           ['B', 'MANISH ', "None", "None"], 
           ['C', 'SHIV ', "None", "None"], 
           ['D', ' GAUR', "None", "None"]]
rwqw0loc

rwqw0loc1#

您应该查看Javascript中的map函数:

let a = [["a","c"],["b","d"]]
a.map(x => x[1])

输出:

Array [ "c", "d" ]
7cwmlq89

7cwmlq892#

您可以循环遍历数组,然后循环遍历其后的每个数组

for (var i = 0; i < arr.length; i++) {  }
     for (var j = 0; j < arr[i].length; j++)
          console.log(arr[i][j]);
lztngnrs

lztngnrs3#

代码:-

var myStringArray = [['A', 'PARIKH', "None", "None"], 
           ['B', 'MANISH ', "None", "None"], 
           ['C', 'SHIV ', "None", "None"], 
           ['D', ' GAUR', "None", "None"]] 
// Accessing by list by list
for (var i = 0; i < myStringArray.length; i++) {
    console.log(myStringArray[i]);  
}

// Accessing by element by element in the list
for (var i = 0; i < myStringArray.length; i++) {
    for (var j=0; j<myStringArray[i].length; j++){
    console.log(myStringArray[i][j]);
    }
}

输出:-

[ 'A', 'PARIKH', 'None', 'None' ]
[ 'B', 'MANISH ', 'None', 'None' ]
[ 'C', 'SHIV ', 'None', 'None' ]
[ 'D', ' GAUR', 'None', 'None' ]
A
PARIKH
None
None
B
MANISH 
None
None
C
SHIV 
None
None
D
GAUR
None
None

相关问题