从Javascript数组中删除重复值,但仅删除重复超过5次的值

从Javascript数组中删除重复值,但仅删除重复超过5次的值,javascript,arrays,Javascript,Arrays,我有一个大数据阵列,我需要一种可靠的方法来提取适当的价值,并实现重复数据消除一体化 由于公平值被放入数组中,我需要从数组中提取至少出现5次或更多次的值 以下是我得到的: var data=['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7'] var uniqueArray = data.filter(function(elem, pos) { return data.indexOf(elem)

我有一个大数据阵列,我需要一种可靠的方法来提取适当的价值,并实现重复数据消除一体化

由于公平值被放入数组中,我需要从数组中提取至少出现5次或更多次的值

以下是我得到的:

var data=['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7']

var uniqueArray = data.filter(function(elem, pos) {
    return data.indexOf(elem) == pos;
});
for (var i=0; i<uniqueArray.length;i++){
    console.log(uniqueArray); 
};
var数据=['1','1','1','1','1','1','5','5','5','6','7','7','7','7','7','7']
var uniqueArray=data.filter(函数(elem,pos){
返回数据。indexOf(elem)==pos;
});
对于(var i=0;i 5;
});

对于(var i=0;i首先创建一个包含值及其出现次数的
map
,然后根据出现次数从
map
中提取值

var数据=['1','1','1','1','1','5','5','5','6','7','7','7']
var映射=数据减少(功能(acc,项目){
如果(附件[项目])附件[项目]+=1;
其他会计科目[项目]=1;
返回acc;
}, {});
var result=Object.keys(映射).filter(函数(键){
返回映射[键]>=5;
})

console.log(result);
首先创建一个包含值及其出现次数的
映射,然后根据出现次数从
映射中提取值

var数据=['1','1','1','1','1','5','5','5','6','7','7','7']
var映射=数据减少(功能(acc,项目){
如果(附件[项目])附件[项目]+=1;
其他会计科目[项目]=1;
返回acc;
}, {});
var result=Object.keys(映射).filter(函数(键){
返回映射[键]>=5;
})

console.log(result);
一个简单的解决方案是构建一个索引(在下面的代码段中命名为
counts
),该索引表示数组中每个值存在的次数:

const data=['1','1','1','1','1','1','5','5','5','6','7','7','7','7','7'];
常量计数=数据。减少((结果,值)=>{
结果[值]=(结果[值]| | 0)+1;
返回结果;
}, {});
const result=Object.keys(counts).filter(value=>counts[value]>=5);

console.log(result);
一个简单的解决方案是构建一个索引(在下面的代码段中命名为
counts
),该索引表示数组中每个值存在的次数:

const data=['1','1','1','1','1','1','5','5','5','6','7','7','7','7','7'];
常量计数=数据。减少((结果,值)=>{
结果[值]=(结果[值]| | 0)+1;
返回结果;
}, {});
const result=Object.keys(counts).filter(value=>counts[value]>=5);
console.log(结果);
试试这个

var data=['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7']

// Create an index mapping of format <value in data array>: <number of occurances>
var map = data.reduce((map, value)=>{
   if (map[value]) 
      map[value]++ // If occurred one or more times, we increment the occurrence count
   else
      map[value] = 1 // If first time occurrence, we set number of occurrences to 1
   return map // return updated index map to reducer
}, {})

// Then we iterate through the index mapping
Object.keys(map).forEach((key)=>{
   if(map[key] > 5) console.log(key) // Note: Here we should print the  key, not the map[key]
})
var数据=['1','1','1','1','1','1','5','5','5','6','7','7','7','7','7','7']
//创建以下格式的索引映射:
变量映射=数据。减少((映射,值)=>{
if(映射[值])
map[value]++//如果发生一次或多次,则增加发生次数
其他的
map[value]=1//如果第一次出现,我们将出现次数设置为1
返回映射//将更新的索引映射返回到reducer
}, {})
//然后我们迭代索引映射
Object.keys(map.forEach)((key)=>{
if(map[key]>5)console.log(key)//注意:这里我们应该打印key,而不是map[key]
})
试试这个

var data=['1','1','1','1','1','1','1','5','5','5','5','6','7','7','7','7','7','7']

// Create an index mapping of format <value in data array>: <number of occurances>
var map = data.reduce((map, value)=>{
   if (map[value]) 
      map[value]++ // If occurred one or more times, we increment the occurrence count
   else
      map[value] = 1 // If first time occurrence, we set number of occurrences to 1
   return map // return updated index map to reducer
}, {})

// Then we iterate through the index mapping
Object.keys(map).forEach((key)=>{
   if(map[key] > 5) console.log(key) // Note: Here we should print the  key, not the map[key]
})
var数据=['1','1','1','1','1','1','5','5','5','6','7','7','7','7','7','7']
//创建以下格式的索引映射:
变量映射=数据。减少((映射,值)=>{
if(映射[值])
map[value]++//如果发生一次或多次,则增加发生次数
其他的
map[value]=1//如果第一次出现,我们将出现次数设置为1
返回映射//将更新的索引映射返回到reducer
}, {})
//然后我们迭代索引映射
Object.keys(map.forEach)((key)=>{
if(map[key]>5)console.log(key)//注意:这里我们应该打印key,而不是map[key]
})

实现一个哈希表!您需要5个索引,但索引多于5个。实现一个哈希表!您需要5个索引,但索引多于5个。这似乎很有效!!!非常感谢!这段代码在性能方面如何?这是一个非常简单的解决方案吗?好吧,您在创建索引对象时会有额外的开销。(目前所有问题的答案都使用这种方法。)如果您的数组总是像示例中那样排序,那么就有可能提出一个更具过程性的解决方案,从而消除对索引对象的需要。这似乎确实有效!!!非常感谢!这段代码在性能方面如何?这是一个非常简单的解决方案吗?好吧,您在创建ind时会有额外的开销ex对象。(目前所有问题的答案都使用这种方法。)如果数组总是像示例中那样进行排序,那么就有可能提出一种更加程序化的解决方案,从而消除对索引对象的需要。