0

I have an array of objects with students name which are getting repeated often. I want to create a frequency counter of the students name with the number of total counts.

Array :

arr = [
    {
        name: 'Akshay',
        age: '15',
    },
    {
        name: 'Rajat',
        age: '14',
    },
    {
        name: 'Akshay',
        age: '16',
    },
    {
        name: 'Sam',
        age: '12',
    },
    {
        name: 'Akshay',
        age: '11',
    },
    {
        name: 'Rajat',
        age: '17',
    },
    {
        name: 'Akshay',
        age: '12',
    },
    {
        name: 'Sam',
        age: '18',
    },
    {
        name: 'Sam',
        age: '19',
    }
    
]

I want to get the result like this in an array

result = [
    {
        name: 'Akshay',
        count: 4
    },
    {
        name: 'Rajat',
        count: 2
    },
    {
        name: 'Sam',
        count: 3
    },
]

I tried the below solution but it's not working properly -

const result = arr.reduce((counter,item) => {
    var getItem = item.name;
    counter[getItem] = counter.hasOwnProperty(getItem) ? counter[getItem] : 1;
    return counter;
})

Please help.

2
  • 1
    You should use reduce with an initial value, an empty object. And you should increment the previous value. Commented Feb 22, 2021 at 7:56
  • 1
    Your reduce call is missing the initial value (use {}), and you need to convert the result to the array that you're looking for. Apart from those the approach is fine. Commented Feb 22, 2021 at 7:56

2 Answers 2

1

You need to pass an accumulator in the reduce. In this case pass an empty object as an accumulator , so the accumulator will look like

{
 name:{
  name:someName,
  count:someCount
 },
 name2:{
  name:someName,
  count:someCount
  }
}

Once the accumulator is successfully populated, then use Object.values to get an array

const arr = [{
    name: 'Akshay',
    age: '15',
  },
  {
    name: 'Rajat',
    age: '14',
  },
  {
    name: 'Akshay',
    age: '16',
  },
  {
    name: 'Sam',
    age: '12',
  },
  {
    name: 'Akshay',
    age: '11',
  },
  {
    name: 'Rajat',
    age: '17',
  },
  {
    name: 'Akshay',
    age: '12',
  },
  {
    name: 'Sam',
    age: '18',
  },
  {
    name: 'Sam',
    age: '19',
  }

]

const result = arr.reduce((counter, item) => {
  var getItem = item.name;
  if (counter.hasOwnProperty(getItem)) {
    counter[getItem].count += 1;
  } else {
    counter[getItem] = {
      name: getItem,
      count: 1
    };

  }
  return counter;
}, {});

console.log(Object.values(result))

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

Comments

1

Try this: (you have been missing the initial value)

const count = arr.reduce((prev, cur) => {
  prev[cur.name] = (prev[cur.name] || 0) + 1;
  return prev;
}, {});

console.log(count);

Comments

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.