Is there a JavaScript equivalent of indexOf for Rust arrays?

var fruits = ["Banana", "Orange", "Apple", "Mango"]; var index = fruits.indexOf("Apple"); 
 let fruits = ["Banana", "Orange", "Apple", "Mango"]; let index = fruits.??? 

If there is no equivalent, maybe you can point me in the right direction? I found this example , but for vectors, not arrays.

+3
source share
1 answer

You can use the position method on any iterator. You can get an iterator over an array using the iter() method. Try this as follows:

 let fruits = ["Banana", "Orange", "Apple", "Mango"]; let res1 = fruits.iter().position(|&s| s == "Apple"); let res2 = fruits.iter().position(|&s| s == "Peter"); println!("{:?}", res1); // outputs: Some(2) println!("{:?}", res2); // outputs: None 
+13
source

Source: https://habr.com/ru/post/976374/


All Articles