Home > Mobile >  JS Object filter based on count of unique values
JS Object filter based on count of unique values

Time:12-21

consider the following sample JS Object

const samplejson = [
    {id:'1',value: "AC",},
    {id:'2',value: "AB",},
    {id:'3',value: "AC",},
    {id:'4',value: "AA",},
    {id:'5',value: "AA",},
    {id:'6',value: "AA",},
    {id:'7',value: "AB",},
    {id:'8',value: "AC",},
    {id:'9',value: "AA",},
    {id:'10',value: "AA",},
]

I want to filter the JS object based on the values unique count and descending order like the following

based on the value counts AA - 5, AB - 2 and AC - 3, but I need the output as AA,AC

In react or JS how can achieve this?

CodePudding user response:

You could iterate over any key in the sample data and count each occurrence of a value in a dictionary. Then you can both print the dictionary for a list of amount of occurrences or just the amount of occurrences of one value.

const data = [
    {id:'1',value: "AC",},
    {id:'2',value: "AB",},
    {id:'3',value: "AC",},
    {id:'4',value: "AA",},
    {id:'5',value: "AA",},
    {id:'6',value: "AA",},
    {id:'7',value: "AB",},
    {id:'8',value: "AC",},
    {id:'9',value: "AA",},
    {id:'10',value: "AA",},
]


// make a dict to store and count occurrences
dict = {};

//iterate over every key in your data and keep count in dict
//(initializing it to 1 if it not exists yet)
for(let i = 0; i < data.length; i  ){
  value = data[i].value;
  if(value in dict){
    dict[value]  ;
  }else{
    dict[value] = 1;
  }
}

// here the amount of occurrences are know
console.log(dict);
amountOfAA = dict['AA'];

Output: { AC: 3, AB: 2, AA: 5 } 5

CodePudding user response:

Check this out:

const data = [{id:'1',value: "AC",},{id:'2',value: "AB",},{id:'3',value: "AC",},    {id:'4',value: "AA",},{id:'5',value: "AA",},{id:'6',value: "AA",},{id:'7',value: "AB",},    {id:'8',value: "AC",},{id:'9',value: "AA",},{id:'10',value: "AA",},];

const result = Object.entries(
  data.reduce((acc, { value }) => ({ ...acc, [value]: (acc[value] || 0)   1 }), {})
).sort((a1, a2) => a2[1] - a1[1])
.map(([key]) => key)
.join(',');

console.log(result);

CodePudding user response:

Your desired result can be found using the following steps:

  1. Tally the values.
  2. Sort the value/count pairs by their count in descending order.
  3. Remove the count, leaving only the value.

const items = [
  { id:  '1', value: "AC" },
  { id:  '2', value: "AB" },
  { id:  '3', value: "AC" },
  { id:  '4', value: "AA" },
  { id:  '5', value: "AA" },
  { id:  '6', value: "AA" },
  { id:  '7', value: "AB" },
  { id:  '8', value: "AC" },
  { id:  '9', value: "AA" },
  { id: '10', value: "AA" },
];

const tally = new Map();
for (const { value } of items) {
  if (!tally.has(value)) tally.set(value, 0);
  tally.set(value, tally.get(value)   1);
}

// displaying as object because Map instances show empty in the snippet log
console.log(Object.fromEntries(tally));

const results = Array.from(tally)
  .sort(([,countA], [,countB]) => countB - countA)
  .map(([value]) => value);

console.log(results);

  • Related