我试图在数组中找到 3 个或更多匹配项,但它只匹配前 3 个,而没有匹配数组的其余部分。如果有人可以提供帮助会很棒:)
var grid = [2,2,2,5,5,5,3,3,3,3];
checkResults();
function checkResults(){
var list_matches = []; // store all matches found
var listcurrent = []; // store current
var maxitems = 3;
var last = -1; // last cell
for(let j =0; j < grid.length; ++j){
let item = grid[j];
// check if last is null
if(last == -1){
// add first item
listcurrent.push(item);
last = item;
console.log("Added: "+item);
continue;
}
let wasMatch = false;
// check match
if(item == last){
wasMatch = true;
listcurrent.push(item);
last = item;
console.log("Added Match: "+item);
}
if(!wasMatch){
console.log("Not matched: " + item);
if(listcurrent.length >= maxitems){
list_matches.push(listcurrent);
}
// reset to null
last = -1;
listcurrent = [];
}
}
console.log(list_matches);
console.log("Cols: " + grid.length);
}
预期结果:来自 [2,2,2,5,5,5,3,3,3,3];
0: 222
1:555
2:3333
当前输出是:0:222,就是这样
慕妹3242003
catspeake
慕妹3146593
相关分类