如何使用Map对象查找数组的元素数量

3

我有一段文字,希望能够使用JavaScript的Map对象来统计每个单词的出现次数。

我知道可以使用.get().set()方法来实现,但是我不确定如何具体操作。

以下是我的代码:

let paragraph = `Lorem ipsum donec nisi taciti et elit congue turpis, lobortis 
                 massa suscipit massa est a praesent metus egestas, conubia turpis 
                 in cursus libero pharetra praesent.
                 Per bibendum taciti sit taciti facilisis a bibendum nisl massa non 
                 aliquam sem auctor ipsum eros, massa sed cubilia porta primis 
                 felis elementum non fringilla conubia neque aenean urna.`

// Split the paragraph into an array of individual words.

let words = paragraph.match(/\w+/gi);

let map = new Map();

for (let i = 0; i < words.length; i++) {
  let word = words[i];
  map.set(word, 0);
  // Logic - if (map contains word) {
    map.set(word, count += 1);
  } else {
    map.set(word, 1);
  }
}

console.log(map);

你想知道每个单词有多少个字符吗? - jburtondev
@jburtondev 不完全正确。我想要段落中每个单词的计数。 - AshNaz87
啊,我明白了。谢谢。我已经更新了我的答案。 - jburtondev
5个回答

3

使用Array#reduce与Map。

对于每次迭代,将单词设置到Map中。

a.get("Lorem")将返回一个数字或未定义。 ||处理未定义的情况。然后加1。

Map#set还返回Map对象。

const paragraph = `Lorem ipsum donec nisi taciti et elit congue turpis, lobortis massa suscipit massa est a praesent metus egestas, conubia turpis in cursus libero pharetra praesent.
Per bibendum taciti sit taciti facilisis a bibendum nisl massa non aliquam sem auctor ipsum eros, massa sed cubilia porta primis felis elementum non fringilla conubia neque aenean urna.`

const words = paragraph.match(/\w+/gi);

const res = words.reduce((a,c)=>{
  return a.set(c, (a.get(c)||0) + 1);
}, new Map());

console.log([...res]);


非常感谢! - AshNaz87

0

检查Map是否有该键,如果有,则获取它并加1,否则只需添加它。

let paragraph = `Lorem ipsum donec nisi taciti et elit congue turpis, lobortis massa suscipit massa est a praesent metus egestas, conubia turpis in cursus libero pharetra praesent.
Per bibendum taciti sit taciti facilisis a bibendum nisl massa non aliquam sem auctor ipsum eros, massa sed cubilia porta primis felis elementum non fringilla conubia neque aenean urna.`

// Split the paragraph into an array of individual words.

let words = paragraph.match(/\w+/gi);

let map = new Map();

for (let i = 0; i < words.length; i++) {
    let word = words[i];
    // Logic - if (map contains word) {
    if(map.get(word)){
        map.set(word, map.get(word) + 1);
    } else {
        map.set(word, 1);
    }
}

console.log(map);


0

检查Map是否已经有该键,如果有,则将1添加到特定值中;如果没有,则将新的键值对添加到Map中。

let paragraph = `Lorem ipsum donec nisi taciti et elit congue turpis, lobortis massa suscipit massa est a praesent metus egestas, conubia turpis in cursus libero pharetra praesent.
Per bibendum taciti sit taciti facilisis a bibendum nisl massa non aliquam sem auctor ipsum eros, massa sed cubilia porta primis felis elementum non fringilla conubia neque aenean urna.`


let words = paragraph.match(/\w+/gi);

let map = new Map();

for (let i = 0; i < words.length; i++) {
  let word = words[i];
  if(map.get(word)) {
    map.set(word, map.get(word)+1);
  } else {
    map.set(word, 1);
  }
}

console.log(...map);


0

使用 Map 数据结构,您可以编写您的代码:

const paragraph = "Lorem ipsum donec nisi taciti et elit congue turpis, lobortis massa suscipit massa est a praesent metus egestas, conubia turpis in cursus libero pharetra praesent. Per bibendum taciti sit taciti facilisis a bibendum nisl massa non aliquam sem auctor ipsum eros, massa sed cubilia porta primis felis elementum non fringilla conubia neque aenean urna."

// Split the paragraph into an array of individual words.

let words = paragraph.match(/\w+/gi);

let map = new Map();

for (let i = 0; i < words.length; i++) {
    let word = words[i];
    // Here you check if map already got the word.
    if (map.has(word)) {
        map.set(word, map.get(word) + 1)
    } else {
        map.set(word, 0);
    }
}

console.log(map);

0

在Map中设置值

paragraph.split(" ").forEach((word) => {
    yourMap.set(word, word.length);
});

从映射中提取值

Array.from(myMap.keys()).forEach((key, value) => {
    console.log(`Count of word ${key} is: ${value}`);
});

2
索引与单词计数有什么关联? - Mark
我认为这个问题与“元素计数”有关。也许我错了。 - jburtondev
我已经与提问者澄清,并相应地更新了我的答案。 - jburtondev

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接