简体   繁体   中英

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM