<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>Document</title>
</head>
<body>
<script>
var num=60;
var arr=[60,60,45,78,60,60,60,65,78,98,60,60,34,45,60];
console.log(compareArr(num,arr));
function compareArr(num,arr){
var length=arr.length;
var result=[];
var prev=arr[0];
var start=-1;
var end=-1;
for(var i=0;i<length;i++){
if(arr[i]==num){
if(i==0||arr[i]!=prev){
start=i;
}
if(i==length-1){
end=i;
result.push({"start":start,"end":end});
}
}else{
if(start>-1){
end=i-1;
result.push({"start":start,"end":end});
start=-1
}
}
prev=arr[i]
}
return result
}
</script>
</body>
</html>
Array:数组获取连续相同数值的起始索引
最新推荐文章于 2021-03-26 15:27:59 发布