2015-11-13 43 views
1

好吧,不知道如何提出這個問題,但我試圖將值收集到數組中,但每個值都可能有重複(或附加)的字符串其餘的價值觀,我相信他們總是建立在彼此之上...Javascript/jQuery查找並刪除數組其餘值中的數組值

這是我的意思。

,如果我要輸出數組,這將是這樣的:

[0] => This is a string. 
[1] => here's another string. This is a string. 
[2] => And now there's a third string. here's another string. This is a string. 
[3] => Here's a string that might not follow the pattern. This is a string. 

我想什麼,最終輸出

[0] => This is a string. 
[1] => here's another string. 
[2] => And now there's a third string. 
[3] => Here's a string that might not follow the pattern. 

基本上,任何字符串不能有重複任何其他字符串中的文本。

不知道我是否合理。

編輯:

下面是完整的故事 - 我收集通過PHP IMAP電子郵件與AJAX腳本。我從每封電子郵件中獲得了正文的文本,但不幸的是,無法通過所有電子郵件服務查找引用文本。所以我做的是讓每個電子郵件正文顯示,並且我已經擺脫了任何額外的字符(如直線引用>>字符)。

我現在想要做的是:從第一封電子郵件開始,檢查其他電子郵件,並刪除第一個字符串(如果它在那裏),然後使用第二封電子郵件,檢查其餘電子郵件並刪除該字符串,等等。

這並不完美,但會縮短很多信息。

+2

雙重for循環'indexOf'檢查。根據輸入的不同,輸出可能會非常糟糕。也許這對你來說很合理,對我來說這似乎很奇怪。 – Halcyon

+0

你可以提供你的數據源,你收集你的價值 –

+0

@AnikIslamAbhi,我已經添加了一個解釋的來源。 – ntgCleaner

回答

1
var strings = [ 
    "This is a string.", 
    "here's another string. This is a string.", 
    "And now there's a third string. here's another string. This is a string.", 
    "Here's a string that might not follow the pattern. This is a string." 
]; 

var i, j, pos; 
for (i = 0; i < strings.length; i += 1) { 
    for (j = 0; j < strings.length; j += 1) { 
     if (i === j) { 
      continue; 
     } 
     pos = strings[i].indexOf(strings[j]); 
     if (pos !== -1) { 
      strings[i] = strings[i].substr(0, pos) + strings[i].substr(pos + strings[j].length); 
     } 
    } 
} 

console.log(strings); 
output: [ 
    "This is a string.", 
    "here's another string. ", 
    "And now there's a third string. ", 
    "Here's a string that might not follow the pattern. " 
]; 

請注意,此代碼不會取代多個發生。如果你想要的話,添加一個while循環。

+0

不錯的答案!有一件事,你能修整結果嗎? –

+0

當然:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/Trim – Halcyon

1

編輯: 這將導致問題如果字符串具有特殊的正則表達式字符。沒有正則表達式一個JS全球repalce可以利用splitjoin,(http://www.adequatelygood.com/JS-Find-and-Replace-with-SplitJoin.html)見下文:

的Javascript需要一個正則表達式做全局替換,所以像這樣

var arr = [ 
    'This is a string.', 
    "here's another string. This is a string.", 
    "And now there's a third string. here's another string. This is a string.", 
    "Here's a string that might not follow the pattern. This is a string." 
]; 
for (var i = 0; i < arr.length; i++) { 
    for (j = 0; j < arr.length; j++) { 
     if (j !== i) { 
      arr[i] = arr[i].split(arr[j]).join(""); 
     } 
    } 
} 
console.log(arr); 


[ 
"This is a string.", 
"here's another string.", 
"And now there's a third string.", 
"Here's a string that might not follow the pattern." 
] 

就應更換所有出現

+0

這是有點工作。我有時會得到一個錯誤,說'未捕獲的SyntaxError:無效的正則表達式:/字符串/:不匹配')''。我會研究這個,因爲其他時間這工作得很好 – ntgCleaner

+0

@ntgCleaner請參閱編輯,我忘記了如果你的字符串中有任何特殊字符,它可能會產生一個無效的正則表達式。使用拆分/加入可以解決這個問題 – chiliNUT

2

這將使用舊數組中的每個字符串首次替換該字符串中新數組中的每個字符串,並使用空字符串構建一個新數組。

var arr = [ 
 
    'This is a string.', 
 
    "here's another string. This is a string.", 
 
    "And now there's a third string. here's another string. This is a string.", 
 
    "Here's a string that might not follow the pattern. This is a string." 
 
]; 
 

 
var out = arr.reduce(function(w, s) { 
 
    w.push(w.reduce(function(s1, e) { 
 
    return s1.replace(e, ""); 
 
    }, s).trim()); 
 
    return w; 
 
}, []); 
 

 
alert(out);

+0

這就是你應該怎麼做的。 –

0

使用類似以下。使用jQuery的.map函數遍歷數組。代碼如下

var arr = [ 
      'This is a string.', 
      "here's another string. This is a string.", 
      "And now there's a third string. here's another string. This is a string.", 
      "Here's a string that might not follow the pattern. This is a string." 
     ]; 

    var out=$.map(arr,function (i,v){ 
     return i.substr(0,i.indexOf(".") + 1); 
    }); 
    console.log("value is"+out); 
+0

這會查找句點並刪除它們後面的所有內容。如果string1是「this.is.a.test」呢? – James