JavaScript Count duplicate values in an array and return count with an additional value using Javascript count duplicate values in excel,count duplicate values in sql,

I've got the following array and I'd like to return a new array containing the count of the duplicate ids along with the value of the id:

const things = [
  {
    id: 1,
    title: 'Something',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 2,
    title: 'Another thing',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 3,
    title: 'Yet another thing',
    categoryId: 2,
    categoryTitle: 'Category 2'
  },
  {
    id: 4,
    title: 'One more thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  },
  {
    id: 5,
    title: 'Last thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  }
]

I've managed to put together a simple function that returns the count of duplicate ids (see below), but it also return the id (i.e. 1, 2, 4):

function categoriesCount (things) {
  const thingsMapped = things.map(thing => thing.categoryId)
  return thingsMapped.reduce((map, val) => {
    map[val] = (map[val] || 0) + 1
    return map
  }, {})
}

console.log('categoriesCount', categoriesCount(things))

Returns:

"categoriesCount" Object {
  1: 2,
  2: 1,
  4: 2
}

Whereas I'd like it to return:

"categoriesCount" Object {
  'Category 1': 2,
  'Category 2': 1,
  'Category 3': 2
}

Note: the category title's numeric value (e.g. Category 3) may not match it's id value (e.g. 4 with regards to Category 3).

What am I missing?

Many thanks in advance.

Answer:1

You could take categoryTitle directly without mapping the array before.

function categoriesCount (things) {
    return things.reduce((hash, { categoryTitle }) => {
        hash[categoryTitle] = (hash[categoryTitle] || 0) + 1;
        return hash;
   }, {});
}

const things = [{ id: 1, title: 'Something', categoryId: 1, categoryTitle: 'Category 1' }, { id: 2, title: 'Another thing', categoryId: 1, categoryTitle: 'Category 1' }, { id: 3, title: 'Yet another thing', categoryId: 2, categoryTitle: 'Category 2' }, { id: 4, title: 'One more thing', categoryId: 4, categoryTitle: 'Category 3' }, { id: 5, title: 'Last thing', categoryId: 4, categoryTitle: 'Category 3' }]

console.log(categoriesCount(things));
.as-console-wrapper { max-height: 100% !important; top: 0; }
Answer:2

You could just do that in a single Array.reduce shot, using destructuring to acquire the desired categoryTitle and to build the key accordingly.

The following code assumes the categoryTitle is consistent along the records and that you want to group by categoryTitle. If you don't want do to such, please specify that.

const things = [
  {
    id: 1,
    title: 'Something',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 2,
    title: 'Another thing',
    categoryId: 1,
    categoryTitle: 'Category 1'
  },
  {
    id: 3,
    title: 'Yet another thing',
    categoryId: 2,
    categoryTitle: 'Category 2'
  },
  {
    id: 4,
    title: 'One more thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  },
  {
    id: 5,
    title: 'Last thing',
    categoryId: 4,
    categoryTitle: 'Category 3'
  }
];

const res = things.reduce((acc, {categoryTitle}) => {
   return acc[categoryTitle] = (acc[categoryTitle] || 0) + 1, acc;
}, {});
console.log('categoriesCount', res);
Answer:3

Assuming your Category Title will be consistent

function categoriesCount (things) {
const thingsMapped = things.map(thing => thing.categoryId)
  return thingsMapped.reduce((map, val) => {
    map[val] = (map[val] || 0) + 1
    return map
  }, {})
}
Answer:4

my goal for this code is to make it so when one of the tree branches is clicked, it changes the clicked ^ from a ^ to a O using JQuery and Javascript (this is for a school assignment). For example, if ...

my goal for this code is to make it so when one of the tree branches is clicked, it changes the clicked ^ from a ^ to a O using JQuery and Javascript (this is for a school assignment). For example, if ...

I am trying to learn how to use prototypes. I have tried 4 different ways to have my text area and button appear but I cannot figure out why they will not appear. I have commented out the 4 different ...

I am trying to learn how to use prototypes. I have tried 4 different ways to have my text area and button appear but I cannot figure out why they will not appear. I have commented out the 4 different ...

I have an array of objects. In the each object is also another array. Structure: function getData() { return [ { _id: 1, tags: ["tomato", "butter", "milk"], title: "How to ...

I have an array of objects. In the each object is also another array. Structure: function getData() { return [ { _id: 1, tags: ["tomato", "butter", "milk"], title: "How to ...

  1. specific data to
  2. extract specific data from text file python
  3. retrieve specific data from firebase android
  4. extract specific data from website using java
  5. extract specific data from text file
  6. get specific data from json
  7. get specific data from firebase
  8. extract specific data from excel
  9. extract specific data from excel cell
  10. retrieve specific data from firebase
  11. scrape specific data from website
  12. remove specific data from array javascript
  13. read specific data from file c++
  14. get specific data from json python
  15. get specific data from json javascript
  16. extract specific data from pdf
  17. get specific data from json c#
  18. extract specific data from json python
  19. retrieve specific data from firebase javascript
  20. get specific data from json php

I'm trying to implement a worker module into my app. The app itself is using require.js to load all the pre-requesites. And this is where all the issues with loading worker-module begin. index.html ...

I'm trying to implement a worker module into my app. The app itself is using require.js to load all the pre-requesites. And this is where all the issues with loading worker-module begin. index.html ...