1

我试图在数组中找到 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,仅此而已

4

4 回答 4

0

使用 Array.prototype[reduce/map/filter] 的另一种解决方案

const someArray = [2, 2, 2, 5, 5, 5, 3, 3, 3, 3, 9, 9];
console.log(aggregate(someArray));

function aggregate(arr) {
  return arr
    // retrieve unique values
    .reduce((acc, val) => !acc.includes(val) && acc.concat(val) || acc, [])
    // use unique values to map arr values to strings 
    // if number of matches >= 3
    .map(val => {
      const filtered = arr.filter(v => v == val);
      return filtered.length > 2 ? filtered.join("") : false
     })
     // filter non falsy values
     .filter(val => val);
}

于 2019-11-11T20:36:47.863 回答
0

你可以这样做:

var grid = [ 1, 1, 2, 3, 4, 5 ];
var hashMap = {};


for( var i = 0; i < grid.length; i++ ) {


  if( hashMap.hasOwnProperty( grid[i] ) ) {
    hashMap[ grid[i] ]++;
  } else {
    hashMap[ grid[i] ] = 1;
  }

}
于 2019-11-11T20:22:31.973 回答
0

如果长度具有所需的最小长度,您可以使用一个临时数组来收集相同的值并推送该数组。

function getMore(array, min) {
    var result = [],
        temp;

    array.forEach((v, i, a) => {
        if (v !== a[i - 1]) return temp = [v];
        temp.push(v);
        if (temp.length === min) result.push(temp);
    });
    return result;
}

console.log(getMore([2, 2, 2, 5, 5, 5, 3, 3, 3, 3], 3));

于 2019-11-11T20:26:35.957 回答
0

它会帮助你。

//toLowerCase for get unique on words, not on character. if i ignore this, it will return two words=> developers. and developers
//first Split and join for remove '.' character form text and finally last split is for convert string to an Array of words that splited by Space character


let uniqWords = Array.from(new Set(text));
//using Set to get unique words and convert it to Array to do more on Array.

let count = {};
// declare varriable for store counts of words.

uniqWords.map(item => {
    count[item] = text.filter(elem => {
        //create property with the name of words and filter common words to an Array 
        return elem == item
    }).length
    //get Array length for get words repeated count.
})

console.table(count)
//log Result into Console 
于 2019-11-11T20:29:08.317 回答