jeudi 29 octobre 2020

how to search through array with if statement and loop in Java Scrpit?

How can I write code like this Python code:

array = [["item1", "item2"], [1, 2]]
x = ["item1", 3]
for i in range(len(array)):
    for j in range(len(x)):
        if x[j] in array[i]:
           # do something

with JavaScript?

I did write this code with JavaScript:

var arr = [["item1", "item2"], [1, 2]],
     x = ["item1", 3];

for (var i = 0; i <arr.length; i++) {
    for (var j = 0; j < x.length; j++) {
         if (x[j] in arr[i]) {
               // do something
          }
    }
}

But the code didn't give me the result I want.

Aucun commentaire:

Enregistrer un commentaire