Javascript 匹配数组中的3个或更多相同元素并将其添加到列表中

Javascript 匹配数组中的3个或更多相同元素并将其添加到列表中,javascript,Javascript,我试图在数组中找到3个或更多匹配项,但它只匹配前3个,而对数组的其余部分不匹配。如果有人能帮忙,那就太好了: var网格=[2,2,2,5,5,3,3,3]; 检查结果; 函数检查结果{ var list_matches=[];//存储找到的所有匹配项 var listcurrent=[];//存储当前值 var maxitems=3; var last=-1;//最后一个单元格 forlet j=0;j=maxitems{ list_matches.pushlistcurrent; } //重

我试图在数组中找到3个或更多匹配项,但它只匹配前3个,而对数组的其余部分不匹配。如果有人能帮忙,那就太好了:

var网格=[2,2,2,5,5,3,3,3]; 检查结果; 函数检查结果{ var list_matches=[];//存储找到的所有匹配项 var listcurrent=[];//存储当前值 var maxitems=3; var last=-1;//最后一个单元格 forlet j=0;j=maxitems{ list_matches.pushlistcurrent; } //重置为空 last=-1; listcurrent=[]; } } console.loglist_匹配; console.logCols:+grid.length; } 预期结果:从[2,2,2,5,5,5,3,3,3]

0:222

1:555

2:3333

电流输出为:
0:222就这样

你可以这样做:

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;
  }

}

您可以这样做:

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;
  }

}

您可以使用一个临时数组来收集相同的值,并在长度达到所需的最小长度时推送此数组

函数getMorearray,最小值{ var结果=[], 临时雇员 array.forEachv,i,a=>{ 如果v!==a[i-1]返回温度=[v]; 温度pushv; 如果temp.length==min result.pushtemp; }; 返回结果; }
loggetMore[2,2,2,5,5,5,3,3,3,3],3 您可以使用一个临时数组来收集相同的值,并在长度达到所需的最小长度时推送此数组

函数getMorearray,最小值{ var结果=[], 临时雇员 array.forEachv,i,a=>{ 如果v!==a[i-1]返回温度=[v]; 温度pushv; 如果temp.length==min result.pushtemp; }; 返回结果; } loggetMore[2,2,2,5,5,5,3,3,3,3],3 它会帮助你的

//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 
它会帮助你的

//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 

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

常量someArray=[2,2,2,5,5,5,3,3,3,9]; console.logaggregatesomeArray; 函数聚合器{ 返回arr //检索唯一值 .reduceacc,val=>!acc.includeval和acc.concatval | | acc,[] //使用唯一值将arr值映射到字符串 //如果匹配数>=3 .mapval=>{ 常量过滤=arr.filterv=>v==val; 返回filtered.length>2?filtered.join:false } //过滤非虚假值 .filterval=>val;
} 另一个使用Array.prototype[reduce/map/filter]的解决方案

常量someArray=[2,2,2,5,5,5,3,3,3,9]; console.logaggregatesomeArray; 函数聚合器{ 返回arr //检索唯一值 .reduceacc,val=>!acc.includeval和acc.concatval | | acc,[] //使用唯一值将arr值映射到字符串 //如果匹配数>=3 .mapval=>{ 常量过滤=arr.filterv=>v==val; 返回filtered.length>2?filtered.join:false } //过滤非虚假值 .filterval=>val; }