1

I have an array which is populated based on the values pulled in from an api call. the array would be having values like this

["9777", "9777", "2.4", "9777", "2.4", "2.4", "9777", "2.4", "2.4", "9777", "9777", "2.4", "2.4", "2.4"]

What I am trying to do is get the count of the occurences of each item in the array and sort it descending based on the count.

I did this which I got it from stackoverflow:

data.forEach(function (x) {
  counts[x] = (counts[x] || 0) + 1;
});

It works, but it gives weird results which makes it difficult to extract the value from the result. here are the results: enter image description here

4
  • How are the results weird? What results do you want? Commented Jun 3, 2018 at 1:55
  • How do I extract the item name and then the count of that item, also how do i sort it based on the count Commented Jun 3, 2018 at 1:56
  • It sounds like you want an object, which isn't sortable, however, an array of objects is sortable. Commented Jun 3, 2018 at 1:58
  • Okay, would you be able to give me an example Commented Jun 3, 2018 at 1:59

2 Answers 2

2

An alternative is using the function reduce to count and create an object with the counts and then use the function sort.

If you want to extract a specific object by the "name", you can use the function find:

let array = ["9777", "9777", "2.4", "9777", "2.4", "2.4", "9777", "2.4", "2.4", "9777", "9777", "2.4", "2.4", "2.4"],
    counts = Object.values(array.reduce((a, c) => {
      (a[c] || (a[c] = {name: c, count: 0})).count += 1;
      return a;
    }, {})).sort(({count: ac}, {count: bc}) => bc - ac),
    target = "2.4",
    found = counts.find(({name}) => name === target);

console.log(counts);
console.log(found);
console.log(found.count);
.as-console-wrapper { max-height: 100% !important; top: 0; }

Sign up to request clarification or add additional context in comments.

2 Comments

Hello @ele Thanks for the code, the reduce works perfectly, but the sorting it should be on the results of the reduce operation. For e.g if the item "2.4" is more than "9777" in the array then the the item item "2.4" should be the first item on the sorted array with its count as well.
It is working right now, now how do i extract the item name and the count of that item for the view. I did counts[0] gave me {2.4: 8}. How to extract just 2.4 and then its count by itself
1

First thing to do would be to get a unique list of items, then loop through that list to add to the final result.

Once the list is generated, we can sort the list using the key count that we created from the previous action.

const items = ["9777", "9777", "2.4", "9777", "2.4", "2.4", "9777", "2.4", "2.4", "9777", "9777", "2.4", "2.4", "2.4"];

// A place to store the results
const result = [];

// Create a unique list of items to loop over
// Add each item to the result list
[...new Set(items)].forEach(item => result.push({
  key: item,
  // Get the count of items of the current type
  count: items.filter(i => i == item).length
}));

// Sort the array from highest to lowest
result.sort((a, b) => b.count - a.count);

console.log(result);

9 Comments

Thanks @Get Off My Lawn. Just wondering what would be [...new Set, it gives me a squiggly.
That is the es6 way of creating a unique list of items.
your tsconfig.json file must have a target of es6 or es2015 or greater.
Si I just need to change that there, I do see it as "target":es5
I change it to es6 on tsconfig.json, all the squigglies are gone, but I do get a compile error on console. I have uploaded the pic of the error, please check.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.