3

imagine I have an object

teaherList = [
{teacherID:1,teacherName:"john"},
{teacherID:2,teacherName:"joe"},
{teacherID:3,teacherName:"jill"},
{teacherID:1,teacherName:"john"},
{teacherID:2,teacherName:"joe"},
{teacherID:3,teacherName:"jill"},
{teacherID:1,teacherName:"john"},
{teacherID:2,teacherName:"joe"},
{teacherID:3,teacherName:"jill"},
]

now how do i find the frequency of each [teacherID:,teacherName:], in the object teaherList

currently how I am doing is,

let temp = []
_.each(teaherList, function(k){
   temp.push(k.teacherID)
)

let count1 = countBy(temp);

well it is giving the frequency of the occurrence of the teachers in the object but is there a better and performant way to do this task

5 Answers5

5

Assuming teaherList is meant to be an array of objects, here's a method that doesn't require depending on a library, and also creates the output object in one go (total iterations = length of array), with reduce:

const teaherList = [
  {teacherID:1,teacherName:"john"},
  {teacherID:2,teacherName:"joe"},
  {teacherID:3,teacherName:"jill"},
  {teacherID:1,teacherName:"john"},
  {teacherID:2,teacherName:"joe"},
  {teacherID:3,teacherName:"jill"},
  {teacherID:1,teacherName:"john"},
  {teacherID:2,teacherName:"joe"},
  {teacherID:3,teacherName:"jill"},
];
console.log(
  teaherList.reduce((a, { teacherName }) => (
    Object.assign(a, { [teacherName]: (a[teacherName] || 0) + 1 })
  ), {})
);
CertainPerformance
  • 356,069
  • 52
  • 309
  • 320
1
let temp = []
_.each(teaherList, function(k){
   temp.push(k.teacherID)
)

let count1 = countBy(temp);
console.log(count1) // object
//(1:3,2:3,3:3)

please let me know if there is a better way around this

1

You could use Array#forEach without overhead for assign the object and returning the object for every loop.

var teacherList = [{ teacherID: 1, teacherName: "john" }, { teacherID: 2, teacherName: "joe" }, { teacherID: 3, teacherName: "jill" }, { teacherID: 1, teacherName: "john" }, { teacherID: 2, teacherName: "joe" }, { teacherID: 3, teacherName: "jill" }, { teacherID: 1, teacherName: "john" }, { teacherID: 2, teacherName: "joe" }, { teacherID: 3, teacherName: "jill" }],
    frequency = Object.create(null);
    
teacherList.forEach(({ teacherName }) => frequency[teacherName] = (frequency[teacherName] || 0) + 1);

console.log(frequency);
Nina Scholz
  • 376,160
  • 25
  • 347
  • 392
1

Here's another way to go about this using a while loop.

teaherList = [{teacherID:1,teacherName:"john"},{teacherID:2,teacherName:"joe"},{teacherID:3,teacherName:"jill"},{teacherID:1,teacherName:"john"},{teacherID:2,teacherName:"joe"},{teacherID:3,teacherName:"jill"},{teacherID:1,teacherName:"john"},{teacherID:2,teacherName:"joe"},{teacherID:3,teacherName:"jill"}];

const calculateFrequencies = ({ input, output = Object.create(null), id, name, 
                                start: i = 0, end = input.length }) => {
    while(i < end && ({ teacherID: id, teacherName: name } = input[i++]))
        ++(output[id] || (output[id] = { id, name, count: 0 })).count;
    
    return Object.values(output);
}

console.log(calculateFrequencies({ input: teaherList }));
0

I modified this teaherList a bit, because that is not the valid object. You can try something like this, resultant array will be like this [{teacherID1: teacherName1}, {teacherID2: teacherName2}].

var teaherList = [
{teacherID:1,teacherName:"john"},
{teacherID:2,teacherName:"joe"},
{teacherID:3,teacherName:"jill"},
{teacherID:1,teacherName:"john"},
{teacherID:2,teacherName:"joe"},
{teacherID:3,teacherName:"jill"},
{teacherID:1,teacherName:"john"},
{teacherID:2,teacherName:"joe"},
{teacherID:3,teacherName:"jill"},
]
var result = teaherList.reduce(function(acc, cV) {
if(!acc[cV.teacherName]) {
acc[cV.teacherName]= {teacherID: cV.teacherID,
teacherName: cV.teacherName, 
count: 1};
} else {
acc[cV.teacherName].count++;
}
return acc;
}, {});

console.log(result);

Hope this helps.

Abhishek
  • 382
  • 1
  • 6
  • hey mate the code is just binding the key to its value the desired out is something similar to [{teacherID:1 teacherName:"john", count:3}, {teacherID:2 teacherName:"Joe", count:3}, {teacherID:3 teacherName:"jill", count:3}] – Naveen Kariyappa Jul 13 '18 at 06:02
  • @NaveenKariyappa got it!! I'll try to get it. – Abhishek Jul 14 '18 at 17:32