【问题标题】:Value of "Key" : "Value" object incorrectly counting“键”的值:“值”对象计数不正确
【发布时间】:2015-05-22 20:46:43
【问题描述】:

我有一个带有句子的数组:

term = ["This is some string","This is another string"]; 

我希望能够按单个单词进行拆分,并计算每个单词在整个 term 数组中出现的次数。

简而言之,我想要:

["This is some string", "This is another string"];

最终变成:

{
    This : 2
    is : 2
    some : 1
    another : 1
    string : 2
}               //(they would be in alphabetic order)


我是这样做的
  1. term.split([" "]); 将每个术语数组句子拆分 用它的话
  2. 将这些单词中的每一个添加到一个对象(或可以称为“关联数组”的对象
  3. 测试单词是否已经在对象中,如果是则增加其值,否则将其添加到值为 1 的对象中。

从下面测试我的代码来看,这似乎在大多数情况下都能正常运行,但是 对于 一些 字,最终计数不正确(我没有得到但是有任何错误!)。
下面的代码不能正确输出计数是否有任何特殊原因?

代码:

var wordsArray{};
var term = ["This is some string", "This is another string"];
    for (var x = 0; x < term.length; x++){
        var splitted = term.split([" "]);
        for (var i = 0; i < splitted.length; i++) { //i = each splitted string(each word)
            var count = 1;
            if (splitted[i] in wordsArray){
                //Add one to key value in wordarray
                wordsArray[splitted[i]]++;                       
            } else {
                wordsArray[splitted[i]] = count;
            }                    
        }
    }

我已经把我的过程相当简单地放在上面,但作为进一步的背景,每个初始句子字符串都来自许多迭代的 json 文件,并且这个词的 映射 包括 all 所有 json文件的strong>句。 (我没有在此处包含此代码,因为我认为它对问题没有任何影响。
这会对计数结果有任何明显的影响吗?

【问题讨论】:

  • 您能否提供一个“失败”的字符串示例并描述所需的输出与生成的输出?
  • 你为什么使用split([" "])[" "] 被字符串化为 " "。所以只需使用split(" ")
  • 注意"toString" in {} 是真的...你应该使用Object.create(null) 创建wordsArray
  • 在控制台中抛出的代码中有一些明显的错误?
  • 发现问题。添加到wordlist 的方法是在错误的循环中,它只是为 1 个 json 文件完成此操作,而不是全部。明显的错误……我想该睡觉了。我会在一分钟内删除这个问题。

标签: javascript arrays iteration


【解决方案1】:

您需要对术语中的每个元素应用拆分。改变这一行

    var splitted = term.split([" "]);

    var splitted = term[x].split([" "]);

所以整个事情看起来像

var wordsArray = {};
var term = ["This is some string", "This is another string"];
    for (var x = 0; x < term.length; x++){
        var splitted = term[x].split([" "]);
        for (var i = 0; i < splitted.length; i++) { //i = each splitted string(each word)
            var count = 1;
            if (splitted[i] in wordsArray){
                //Add one to key value in wordarray
                wordsArray[splitted[i]]++;                       
            } else {
                wordsArray[splitted[i]] = count;
            }                    
        }
    }

console.log(wordsArray)

【讨论】:

    【解决方案2】:

    一个jQuery方法的做会如下。

    var wordsArray = {};
    var term = ["This is some string", "This is another string"];
    
    $.each(term, function (index, value) {
        var split = value.split(" ");
    
        $.each(split, function (i, v) {
           wordsArray[v] = v.length; 
        });
    });
    
    console.log(wordsArray);
    

    这是一个不同的单词数组,键是单词,值是计数。

    输出示例:

    Object {This: 4, is: 2, some: 4, string: 6, another: 7}
    

    【讨论】:

      【解决方案3】:

      正如其他人所说,您的示例代码是错误的:第 4 行应该是

      var splitted = term[x].split([" "]); // term[x]
      

      字数错误的一个原因可能是,您似乎只将空格视为分隔符。

      • 点、逗号、制表符、换行符等其他字符呢?
      • 而且大多数情况下,它确实将“this”和“This”视为单独的词(区分大小写!)

      例子:

      var term = ["This is some string.", "this is \"another\" string?"];
      

      生产

      "another": 1
      this: 1
      This: 1
      is: 2
      some: 1
      string.: 1
      string?: 1
      

      【讨论】:

        猜你喜欢
        • 2019-10-22
        • 2021-01-29
        • 2021-10-21
        • 1970-01-01
        • 2018-09-22
        • 1970-01-01
        • 2017-08-12
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多