2014-03-06 54 views
0

我想計算字符在文本中出現的次數,然後顯示每個字符的總計。我的主要問題是第一部分。基於另一個屬性設置嵌套對象屬性

這裏是我的代碼快照至今:

//define text and characters to be searched for 
var theArticle = document.getElementById('theText'), 
    docFrag = document.createDocumentFragment(), 
    theText = theArticle.innerText, 
    characters = { 
    a: { 
     regExp: /a/g, 
     matches: [], 
     count: 0 
    }, 
    b: { 
     regExp: /b/g, 
     matches: [], 
     count: 0 
    }, 
    c: { 
     regExp: /c/g, 
     matches: [], 
     count: 0 
    } 

    etc… 
} 

for (var key in characters) { 
    if (characters.hasOwnProperty(key)) { 
     var obj = characters.key; 
     for (var prop in obj) { 
      if (obj.hasOwnProperty(prop)) { 
       matches = theText.match(regExp); // pretty sure my problem is here 
       count = matches.length; // and here 
      } 
     } 
    } 
} 

我想遍歷所有的字符集的基礎上matches長度基於regExpmatches的價值和count值。

這個問題的最佳答案是最接近我解決我的問題。 Access/process (nested) objects, arrays or JSON

+0

我已經可以發現有兩件事情錯了:1)沒有必要的檢查:'如果(characters.hasOwnProperty(密鑰))'裏面你'爲in'循環。 2)你不能用點符號訪問一個變量屬性:'var obj = characters.key;'應該是'var obj = characters [key];'。 3)同樣,不需要檢查:'if(obj.hasOwnProperty(prop))'。糾正這些事情(主要是#2),它應該按照預期工作。 – tenub

+0

嗯,我認爲解決了這個問題,但我想我在Chrome控制檯中出現了誤報。 – joel

回答

0

有跡象表明,應該對你的代碼進行一些更改:

  1. 沒有必要的檢查:if (characters.hasOwnProperty(key))for in循環內。
  2. 用點表示法不能訪問變量屬性:var obj = characters.key;應該是var obj = characters[key];
  3. 同樣,不需要檢查:if (obj.hasOwnProperty(prop))
  4. 如果您嘗試操作字符對象屬性,則需要訪問對象上的屬性,而不只是輸入密鑰名稱。

糾正這些事情(主要是#2),它應該按預期工作。

不過,我將完全重新裝備你的功能的實現,像這樣:

我先簡單地定義字符串數組:

var characters = ['a','b','c','asd']; 

然後寫一個通用的函數來處理你的功能:

function findCharsInString(array, string) { 
    // map a "results" function to each character in the array 
    return array.map(function(c) { 
     // make a check to ensure we're working with a string that is only one character 
     if (typeof c === 'string' && c.length === 1) { 
      // create a RegExp from each valid array element 
      var matches = string.match(RegExp(c, 'g')); 
      // return an anonymous results object for each character 
      return { 
       matches: matches, 
       count: matches.length 
      }; 
     } else { // if there is an invalid element in the array return something 
      return 'Invalid array element.'; 
     } 
    }); 
} 

並將其應用到你的characters數組和字符串theText

var theText = 'asjdbasccshaskjhsa'; 
console.log(findCharsInString(characters, theText)); 

日誌:

[ 
    { 
     matches: 
      ["a","a","a","a"], 
     count: 
      4 
    }, 
    { 
     matches: 
      ["b"], 
     count: 
      1 
    }, 
    { 
     matches: 
      ["c","c"], 
     count: 
      2 
    }, 
    "Invalid array element." 
] 
+0

這太棒了。謝謝。你怎麼能包括特殊字符,如不間斷空格和標點符號? – joel

+0

我會看看[這裏](http://stackoverflow.com/questions/3115150/how-to-escape-regular-expression-special-characters-using-javascript)轉義'c'在RegExp(c ,'g')'如果你超越了基本的字母數字。然後你需要刪除字符數組的長度檢查。 – tenub

+0

再次,真棒,謝謝。我將其標記爲答案。我唯一需要改變的就是刪除長度檢查(就像你所建議的那樣),並且我在匿名結果對象的頂部添加了'character:c',以使其不那麼匿名。另外,我似乎無法逃避'?'。 – joel

相關問題