2011-10-31 23 views
1

我有CONSOLE.LOG(cMentions)當下列的數組:鑑於陣列如何移除項不包含在一個textarea值(字符串)

Object 
41: "Never Ever" 
43: "Steve Jobs" 
55: "Henry Ford" 
__proto__: Object 

然後,我有一個textarea(我想要做的就是確定cMentions記錄是否在textarea值中。如果他們做X,如果他們不做Y.

用例。當它運行時,它會注意到亨利福特不在文本區域並將其刪除。

想法?

感謝

回答

1

循環每一個元素,並檢查在關鍵的名稱是否在字符串中存在,使用.indexOf

//cMentions is defined as in the question 
var commentValue = $('#comment').val(); 
for (var id in cMentions) { 
    if (cMentions.hasOwnProperty(id)) { // Ignore native methods 
     var searchTerm = cMentions[id]; // Search for the existence of this name 
     if (commentValue.indexOf(searchTerm) == -1) { 
      cMentions[id] = void 0;  // Overwriting by `void 0` = `undefined` 
      delete cMentions[id];  // In case the variable still exists 
     } 
    } 
} 
+0

這很好,但是,使數組項,如下所示:19:未定義---我怎樣才能擺脫陣列中的記錄 –

+1

@RachelaMeadows在我的瀏覽器中,'19'作爲條目消失。我已經更新了答案,如果沒有,使用'delete'。 –

+0

這樣做的伎倆,謝謝你 –

相關問題