在 javascript 中查找对象中键值对的频率
finding the frequency of a key value pair in an object in javascript
假设我有一个对象
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"},
]
现在如何在对象 teaherList
中找到每个 [teacherID:,teacherName:] 的频率]
目前我在做什么,
let temp = []
_.each(teaherList, function(k){
temp.push(k.teacherID)
)
let count1 = countBy(temp);
好吧,它给出了对象中教师出现的频率,但是有没有更好、更高效的方法来完成这项任务
let temp = []
_.each(teaherList, function(k){
temp.push(k.teacherID)
)
let count1 = countBy(temp);
console.log(count1) // object
//(1:3,2:3,3:3)
如果有更好的解决方法,请告诉我
假设teaherList
是一个对象数组,这里有一个不需要依赖于库的方法,并且还一次性创建了输出对象(总迭代次数=数组长度) , 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 })
), {})
);
我修改了这个 teaherList 一点,因为那不是有效的对象。你可以尝试这样的事情,结果数组将是这样的 [{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);
希望对您有所帮助。
您可以使用 Array#forEach
为每个循环分配对象和返回对象而不会产生开销。
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);
这是使用 while 循环的另一种方法。
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 }));
假设我有一个对象
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"},
]
现在如何在对象 teaherList
中找到每个 [teacherID:,teacherName:] 的频率]目前我在做什么,
let temp = []
_.each(teaherList, function(k){
temp.push(k.teacherID)
)
let count1 = countBy(temp);
好吧,它给出了对象中教师出现的频率,但是有没有更好、更高效的方法来完成这项任务
let temp = []
_.each(teaherList, function(k){
temp.push(k.teacherID)
)
let count1 = countBy(temp);
console.log(count1) // object
//(1:3,2:3,3:3)
如果有更好的解决方法,请告诉我
假设teaherList
是一个对象数组,这里有一个不需要依赖于库的方法,并且还一次性创建了输出对象(总迭代次数=数组长度) , 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 })
), {})
);
我修改了这个 teaherList 一点,因为那不是有效的对象。你可以尝试这样的事情,结果数组将是这样的 [{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);
希望对您有所帮助。
您可以使用 Array#forEach
为每个循环分配对象和返回对象而不会产生开销。
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);
这是使用 while 循环的另一种方法。
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 }));