使用两个哈希表来识别 javascript 中的相似性

using two hash tables to identify similarities in javascript

我正在尝试使用哈希表将下面的杂志数组与笔记数组进行比较。我查看了这个问题,发现它可以通过其他方式完成,但我正在尝试专门学习如何使用哈希表。我想看看杂志中是否有与笔记相同的词。我的想法是这样结束然后比较它们

magazineHash = {
    "cool": 2,
    "needs": 1,
    "some": 1,
    "for": 1,
    "work": 1
}

和注释数组相同,然后比较单词的频率(值)

magazine = ["cool", "needs", "some", "for", "work", "cool"];
notes = ["cool", "needs", "for", "cool", "work"]

function reliableNote(magazine, note){

}

人们在网上谈论的关于哈希表的信息太多了,种类繁多,我感到很困惑!任何帮助都会很棒!

如果您想将 array 映射到 object/hash table,您可以使用 reduce 函数:

const magazine = ["cool", "needs", "some", "for", "work", "cool"];
const notes = ["cool", "needs", "for", "cool", "work"]


function mapToHash(arr) {
  return arr.reduce((hash, entry) => ({ ...hash,
    [entry]: hash[entry] ? hash[entry] + 1 : 1
  }), {})
}

console.log(mapToHash(magazine));
console.log(mapToHash(notes));