//在数组中查找所有出现的x,并返回一个包含匹配索引的数组 function findall(a,x){ var results=[], len=a.length, pos=0; while(pos<len){ pos=a.indexOf(x,pos); if(pos===-1){//未找到就退出循环完成搜索 break; } results.push(pos);//找到就存储索引 pos+=1;//并从下个位置开始搜索 } return results; } var arr=[1,2,3,1,4,1,4,1]; findall(arr,1);//返回[0,3,5,7]
The above is the detailed content of JavaScript finds and returns specific elements and indexes in an array. For more information, please follow other related articles on the PHP Chinese website!