Javascript 在js中查找数组中第一个字符串的匹配项

Javascript 在js中查找数组中第一个字符串的匹配项,javascript,Javascript,假设给我一个数组 myArray=[10,true,1.5,'Asia', 20, 'rat']; 现在,我应该使用哪个函数来获取数组中第一个字符串的索引?您可以使用 const result=[10,true,1.5,'Asia',20,'rat']; 控制台日志(结果)如果您想找到字符串的第一个索引,那么这将是最好的选择之一 constmyarray=[10,true,1.5,“Asia”,20,“rat”]; const index=myArray.findIndex((s)=>ty

假设给我一个数组

myArray=[10,true,1.5,'Asia', 20, 'rat'];
现在,我应该使用哪个函数来获取数组中第一个字符串的索引?

您可以使用

const result=[10,true,1.5,'Asia',20,'rat'];

控制台日志(结果)如果您想找到字符串的第一个索引,那么这将是最好的选择之一

constmyarray=[10,true,1.5,“Asia”,20,“rat”];
const index=myArray.findIndex((s)=>typeof s==“string”);

控制台日志(索引)使用
findIndex

constmyarray=[10,true,1.5,'Asia',20,'rat'];
常量索引=myArray.findIndex((el)=>{
返回类型el=='string';
} );

console.log('答案:',索引)通常您可以使用findIndex,但也可以对..
循环和
中断使用normal

让myArray=[10,true,1.5,'Asia',20,'rat'];
设ind=-1;
for(设x=0;xconsole.log(ind)
为了完整起见,这里是
数组.prototype.filter()方法的示例。

使用filter function
findIndex
将是选项之一…具有
类型的filter function