-1

I have a value which is basically set of appended python lists . How can I iterate them in JS .

My value looks like :

 value =  [['A', ' PARIKH', "None", "None"], 
           ['B', 'MANISH ', "None", "None"], 
           ['C', 'SHIV ', "None", "None"], 
           ['D', ' GAUR', "None", "None"]] 
  • 1
    How is this python list represented in your JavaScript program? Putting that code into JS will throw an error (due to the `None`) – Nick Parsons Dec 16 '22 at 08:23
  • Why do you want to loop through a `python` list in `javascript`? I guess you have a somehow serialized structure (`string`, `JSON`, ...) to communicate and do not really want to access th values as known to the python interpreter? In this case, just use a JS loop... – Cpt.Hook Dec 16 '22 at 08:24
  • Assume none as "None" @NickParsons – Surya Singh Dec 16 '22 at 08:26
  • Its just that I have a value in this format . Is there any way to iterate each record .?@Cpt.Hook – Surya Singh Dec 16 '22 at 08:27
  • @SuryaSingh I don't really see what python has to do with this question then. You have a javascript array, which you can loop over using for loops, in a similar way that you can with python. – Nick Parsons Dec 16 '22 at 08:36

3 Answers3

1

You should look at map function in Javascript:

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

output:

Array [ "c", "d" ]
Iman
  • 2,224
  • 15
  • 35
1

You can just loop through the array and then loop through every array after that

for (var i = 0; i < arr.length; i++) {  }
     for (var j = 0; j < arr[i].length; j++)
          console.log(arr[i][j]);
Aiotex
  • 53
  • 1
  • 7
1

Code:-

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]);
    }
}

Output:-

[ '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
Yash Mehta
  • 2,025
  • 3
  • 9
  • 20