在javascript中查找对象中的键值对的频率

时间:2018-07-13 04:39:05

标签: javascript arrays object frequency find-occurrences

想象我有一个对象

df['MyCol'].replace('[\,\s]+\d+.+', '')

现在如何在对象teaherList中找到每个[teacherID: ,teacherName: ]的频率

目前我的状态是

function moveValuesOnly () {
  var ss = SpreadsheetApp.getActiveSpreadsheet ();
  var source = ss.getRange ("PNG Sheet!A1:E1");
  var destSheet = ss.getSheetByName("Project Codes");
  var destRange = destSheet.getRange(destSheet.getLastRow()+1,1);
  source.copyTo (destRange, {contentsOnly: true});
  source.clear ();
}

它给出了教师在对象中出现的频率,但是有一种更好,更有效的方法来完成此任务

5 个答案:

答案 0 :(得分:5)

假设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 })
  ), {})
);

答案 1 :(得分:1)

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"},
]

请让我知道是否有更好的解决方法

答案 2 :(得分:1)

您可以在没有开销的情况下使用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);

答案 3 :(得分:1)

这是使用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 }));

答案 4 :(得分:-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);

希望这会有所帮助。