js 取到相同的字符串 返回对应的下标

var rest=[];
var array=["aaa","aaa","","ddd","eee","eee","eee","eee","eee","eee","yyy","zzz","ggg","ggg"];
array.forEach(function (v,i) {
var sameInLeft=array[i]==array[i-1]?1:0;
var sameInRight=array[i]=array[i+1]?1:0;
if(sameInLeft+sameInRight==1){
rest.push(i);
}

});
for(var i=0;i
var indexArr=[rest[i*2],rest[i*2+1]];
//排除收尾连续
if(indexArr[0]!=0&& indexArr[1]!=(array.length-1)){
console.log(indexArr,array.slice(indexArr[0],indexArr[1]+1))

}

}


结果:


[4, 5] [1, 1]
 [6, 7] [1, 1]
 [8, 9] [1, 1]
 [10, 11] [1, 1]
 [12, undefined]

 

你可能感兴趣的:(js 取到相同的字符串 返回对应的下标)