Closed. This question needs details or clarity。它当前不接受答案。
                            
                        
                    
                
                            
                                
                
                        
                            
                        
                    
                        
                            想改善这个问题吗?添加详细信息并通过editing this post阐明问题。
                        
                        去年关闭。
                                                                                            
                
        
我有一个充满单词的数组,我需要计算用户选择是否忽略大小写对相似单词进行排序的相似单词

function parseText(text,letterCase,punc) {
    var words = text
        .replace(/[.,\/#!$%\^&\*;:{}=\-_`~()]/g, '')
        .split(/\s/); //split the array and remove punctualitis

    words.forEach(function(w) {
        if (!Sub_Word[w]) {
            Sub_Word[w] = 0;
        }

        Sub_Word[w]+=1;//creating an array and the word and how much it was there
    });

    return Sub_Word;
}


现在这段代码可以正常工作了,例如在这种情况下,此数组["he","He","hello"]

我需要它来返回[["he:2"],[hello:1]]

最佳答案

使用array.reduce:



var words = ["he","He","hello"];
var res = words.reduce((m, o) => {
  o = o.toLowerCase();
  var found = m.find(e => e[0] === o);
  found ? found[1]++ : m.push([o, 1]);
  return m;
}, []);
console.log(res);

关于javascript - 计算数组中有无大小写的相似单词,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/51817203/

10-10 10:33