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;x console.log(ind)
为了完整起见,这里是数组.prototype.filter()方法的示例。使用filter functionfindIndex
将是选项之一…具有类型的filter function