Javascript 不知道数组的数目

Javascript 不知道数组的数目,javascript,arrays,Javascript,Arrays,我如何知道数组中的“Januar”在哪个位置(在数字中)^ 最后,我想得到0表示“一月”,1表示“二月” 该方法返回数组中可以找到给定元素的第一个索引,如果该元素不存在,则返回-1 那么你的情况呢 Monat.indexOf("January") 应该返回0,您可以使用Array.prototype来查找索引 var Monat=[“一月”、“二月”]; 函数findIndex(天){ 返回Monat.findIndex((x)=>x==day); } 常数指数=fin

我如何知道数组中的“Januar”在哪个位置(在数字中)^ 最后,我想得到0表示“一月”,1表示“二月”

该方法返回数组中可以找到给定元素的第一个索引,如果该元素不存在,则返回-1

那么你的情况呢

 Monat.indexOf("January")
应该返回0,您可以使用Array.prototype来查找索引

var Monat=[“一月”、“二月”];
函数findIndex(天){
返回Monat.findIndex((x)=>x==day);
}
常数指数=findIndex(“一月”);
控制台日志(索引);
const index2=findIndex(“二月”);

console.log(index2)您可以为此使用indexOf。如果找到,它将返回数组中第一个匹配项的索引,否则返回-1

var Monat=[“一月”、“二月”];
console.log(Monat.indexOf(“一月”)//0
console.log(Monat.indexOf(“二月”)//1.
log(Monat.indexOf(“不存在”)//-我是你的朋友。
 Monat.indexOf("January")