简体   繁体   English

过滤第一个元素在数组中重复两次以上的项目

[英]filter the item whose first element is more than twice repeated in array

I have an array and want to filter the item whose first element is more than twice repeated in this array.我有一个数组,想过滤第一个元素在这个数组中重复两次以上的项目。

Friends, how can I do this?.朋友,我该怎么办?

 arr=[
[a,1],
[a,2],
[a,4],
[b,2],
[C,2],
[d,2],
[e,2],
[e,2],
[n,2],
...
]

I want the result of the filter to be like below我希望过滤器的结果如下所示

 result=[
[a,1],
[a,2],
[a,4],
[e,2],
[e,2],
...
]

Chek this out:看看这个:

arr=[['a',1], ['a',2], ['a',4], ['b',2], ['C',2], ['d',2], ['e',2], ['e',2], ['n',2]];

 const reducedObject = arr
  .reduce((acc, [key, value]) => (
    acc[key] = acc[key] 
      ? [...acc[key], [key, value]] 
      : [[key, value]],
    acc
  ), {});

  console.log(reducedObject);
// {
//   a: [ [ 'a', 1 ], [ 'a', 2 ], [ 'a', 4 ] ],
//   b: [ [ 'b', 2 ] ],
//   C: [ [ 'C', 2 ] ],
//   d: [ [ 'd', 2 ] ],
//   e: [ [ 'e', 2 ], [ 'e', 2 ] ],
//   n: [ [ 'n', 2 ] ]
// }

const result = Object
  .values(reducedObject)
  .filter((values) => (values.length >= 2))
  .reduce((acc, values) => ([...acc, ...values]), []);

console.log(result);
// [ 
//   [ 'a', 1 ], 
//   [ 'a', 2 ], 
//   [ 'a', 4 ], 
//   [ 'e', 2 ], 
//   [ 'e', 2 ] 
// ]

Another, one-line-way solution:另一种单线解决方案:

const result2 = arr
  .map(([ key ]) => key)  //[ 'a', 'a', 'a', 'b', 'C', 'd', 'e', 'e', 'n' ]
  .filter((key, index, array) => array.indexOf(key) !== index)  //[ 'a', 'a', 'e' ]
  .filter((key, index, array) => array.indexOf(key) === index)  //[ 'a', 'e' ]
  .reduce((acc, key) => (  
    [...acc, ...arr.filter(([ itemKey ]) => (itemKey === key))]
  ), []);   
  
console.log(result2);
// [ [ 'a', 1 ], [ 'a', 2 ], [ 'a', 4 ], [ 'e', 2 ], [ 'e', 2 ] ]

 arr=[['a',1],['a',2],['a',4],['b',2],['C',2],['d',2],['e',2],['e',2],['n',2]]; temp=arr.map(val=>val[0]).filter((a,i,aa)=>aa.indexOf(a)===i&&aa.lastIndexOf(a)!==i); result=arr.filter((a, i,aa)=>temp.includes(aa[i][0])); console.log(result)

want to filter the item whose first element is more than twice repeated in this array.想要过滤第一个元素在此数组中重复两次以上的项目。

To do that You need :要做到这一点,你需要:

  1. make a hashed map with elements containing character with its times repeated.使用包含重复次数的字符的元素制作散列映射。
  2. keep only elements from array whose repeated two or more times.只保留数组中重复两次或多次的元素。
const arrays=[['a',1], ['a',2], ['a',4], ['b',2], ['C',2], ['d',2], ['e',2],['e',2], ['n',2]];

//utility function to get keys of each entry
const keys = arr => arr.map(char=>char[0]);

let chars = keys(arrays); // ['a', 'a', 'a', 'b', 'C', 'd', 'e', 'e', 'e', 'n']

// 1.
const hashMap = new Map();
chars.forEach(char=>{
    count = hashMap.get(char)||0;
    hashMap.set(char, ++count);
});
const dict = [...hashMap].filter(([char, count])=>count>=2)

// 2.
uniqueChars = keys(dict);
const result = arrays.filter(([chr, num])=>uniqueChars.includes(chr))
console.log(result)

Result:结果:

['a', 1]
['a', 2]
['a', 4]
['e', 2]
['e', 2]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 具有多个元素的数组过滤器 - Array filter with more than one element 用于显示数组中的一个随机元素的 Html 代码,但不应为超过 1 个用户重复 - Html code to display one random element from an array but should not be repeated for more than 1 user 如果两次过滤相同的数据,为什么一次执行Array.filter比$ filter花费将近十倍的时间? - If filtering same data twice, why does Array.filter takes almost 10 times more time than $filter if executed at once? 如何使用.filter() 搜索多个项目 - How to search for more than one item with .filter() 如何在Vuejs中过滤多个元素 - How to filter by more than one element in Vuejs 谷歌标签管理器模板仅读取数组的第一个元素以获取超过 1 个项目的事件 - google tag manager template only reading first elements of array for events with more than 1 item JS中如何返回第一个索引为最大数的数组元素? - How to return array element whose first index is the largest number in JS? 如何使用for循环返回数组中重复项的首次出现? - How to return the first occurrence of repeated item in an array using for loop? 具有多个条件的Array.filter - Array.filter with more than one conditional 数组第一项的AngularJS Filter属性 - AngularJS Filter property from the first item on an array
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM