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

时间:2019-01-20 17:28:15

标签: javascript javascript-objects

我有一个单词的段落,想查找每个单词的计数,特别是使用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);

5 个答案:

答案 0 :(得分:3)

在地图上使用Array#reduce

为每次迭代在地图中设置单词。

a.get("Lorem")将返回数字或未定义。 ||处理是否未定义。然后添加1.

Map#set还返回地图对象。

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]);

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

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

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

答案 4 :(得分:0)

在地图中设置值

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

从地图中提取值

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