Home > Net >  Filtering Array of Objects on the base of two fields in type script
Filtering Array of Objects on the base of two fields in type script

Time:02-27

I am working in angular 5 I have a problem

I have an API response like:

modules: Array(6) 
0:{name:module1,value:'a',key:10} 
1:{name:module2,value:'b',key:20} 
2:{name:module3,value:'c',key:20} 
3:{name:module1,value:'d',key:10} 
4:{name:module2,value:'e',key:20} 
5:{name:module3,value:'f',key:20}

I want to filter this response like this

modules:[ 
{name:module1,key:10}, 
{name:module2,key:20},
{name:module3,key:20} 
]

basically I want to filter on the basis of 'name' parameter. I want unique name but i also want to get the key parameter associated with each unique name parameter and ignore the value parameter

what I was trying:

const moduleNames = [...Array.from(new Set(this.APIResponse.map(item => item.name)))]; 
const keys = [...Array.from(new Set(this.APIResponse.map(item => item.key)))];

I got

moduleNames :[module1,module2,module3] 
keys : [10,20]

in the keys array I also want 20 to come again for module3 but the set is getting unique elements. and I want to ignore the 'value' parameter

I found a lot of similar questions but nothing worked for my case. I am sorry if this question is repeated but please co-operate thank you.

CodePudding user response:

You can use concatenation of the field values you want to be unique and push them into a map then check for the existence of the combination like below :

const array = [{key: '40', val: 'Behavior', name: 'Module1'} ,{key: '40', val: 'Know', name: 'Module1'} ,{key: '40', val: 'GDE', name: 'Module1'} ,{key: '40', val: 'Evaluation', name: 'Module1'} ,{key: '32', val: 'Knowledge', name: 'Module2'} ,{key: '32', val: 'Driving', name: 'Module2'} ,{key: '32', val: 'Vehicle', name: 'Module2'} ,{key: '32', val: 'RTA', name: 'Module2'} ,{key: '32', val: 'Outraging', name: 'Module2'} ,{key: '32', val: 'Corruption', name: 'Module2'} ,{key: '32', val: 'Evaluation', name: 'Module2'} ,{key: '24', val: 'Mechanical', name: 'Module3'} ,{key: '40', val: 'Road', name: 'Module1'} ,{key: '24', val: 'Active', name: 'Module3'} ,{key: '24', val: 'Eco', name: 'Module3'} ,{key: '24', val: 'ADAS', name: 'Module3'} ,{key: '24', val: 'future', name: 'Module3'} ,{key: '24', val: 'Evaluation', name: 'Module3'} ,{key: '48', val: 'Improving', name: 'Module4'} ,{key: '48', val: 'Improving', name: 'Module4'} ,{key: '48', val: 'Independent', name: 'Module4'} ,{key: '48', val: 'Evaluation', name: 'Module4'} ,{key: '32', val: 'Communication', name: 'Module5'} ,{key: '40', val: 'Adverse', name: 'Module1'} ,{key: '32', val: 'Adult', name: 'Module5'} ,{key: '32', val: 'Learning', name: 'Module5'} ,{key: '32', val: 'Coaching', name: 'Module5'} ,{key: '32', val: 'Personal', name: 'Module5'} ,{key: '32', val: 'Service', name: 'Module5'} ,{key: '32', val: 'Customer', name: 'Module5'} ,{key: '32', val: 'Service', name: 'Module5'} ,{key: '32', val: 'Evaluation', name: 'Module5'} ,{key: '64', val: 'Yard', name: 'Module6'} ,{key: '64', val: 'Safety', name: 'Module6'} ,{key: '40', val: 'Forecasting', name: 'Module1'} ,{key: '64', val: 'Smart', name: 'Module6'} ,{key: '64', val: 'On-road', name: 'Module6'} ,{key: '64', val: 'On-road', name: 'Module6'} ,{key: '64', val: 'CTS', name: 'Module6'} ,{key: '64', val: 'Evaluation', name: 'Module6'} ,{key: '40', val: 'Accident', name: 'Module1'} ,{key: '40', val: 'Defensive', name: 'Module1'} ,{key: '40', val: 'What', name: 'Module1'} ,{key: '40', val: 'First', name: 'Module1'} ,{key: '40', val: 'Theory', name: 'Module1'}];

const map = {};

const filtered = array.map(({name,key})=> {
  const uniqeFilter = `${name}_${key}`
  if (map[uniqeFilter])
    return null;
  map[uniqeFilter] = true;
  return [name,key]
}).filter(r => r);

const modules = filtered.map(([module])=> module);
const keys = filtered.map(([,key])=> key);
console.log(modules,keys);

  • Related