2017-04-19 55 views
0

使用Slack webhooks和webtask.io我已經爲Slack創建了一個通知過濾器(基於我在網上找到的一個通用腳本),在這個時候,過濾掉包含字符串NON-SLA的消息。我對JS完全陌生,所以到此爲止都是試錯。我想調整腳本來過濾掉包含任意數量字符串的消息,但是我處於障礙之中主要是因爲我對語言不熟悉,並且有幸嘗試了各種解決方案,在網上找到了。JS Slack通知過濾器

下面是隻是一個單一的串的電流過濾器:

function shouldNotify(data) { 
    return !data.text.includes('NON-SLA'); 
} 

返回true爲shouldNotify和腳本的其餘部分推消息發送到指定通道。

我無法找到一個方法來做到類似於此:

return !data.text.includes('one','two','three'); 

看着使用數組,但沒有什麼,我發現好像它會與現有的腳本工作作爲一個整體,我只是沒有時間嘗試重寫它。似乎這將是最有效和正確的方式來做到這一點。可以看到完整的腳本here

任何幫助都將不勝感激,因爲我對這個知識的瞭解有限。

TIA

+0

你在尋找'[「one」,「two」,「three」]。some(string =>「one or two or three」.includes(string))'? –

+0

謝謝,但由於我對語言不熟悉,所以我真的不知道。我剛剛嘗試過: return!data.text.includes [「110336」,「103532」,「103405」] some(string =>「110336 or 103532 or 103405」.includes(string)) 但是,不行。 – Tronyx

+0

完美的作品!非常感謝!對不起,我沒有意識到這僅僅是功能性代碼/腳本。猜猜這解釋了我得到的降價。 – Tronyx

回答

0

您可以使用Array.some()

function shouldNotify(data) { 
 
    const string = ["one", "two", "three"]; 
 
    return !string.some(string => data.text.includes(string)); 
 
} 
 

 
// Example: 
 
console.log(shouldNotify({text: "four apples"})); // true 
 
console.log(shouldNotify({text: "two oranges"})); // false

而不是提供一個箭頭功能Array.some,可以交替使用Function.bind,並寫上:

return !string.some(String.includes.bind(string)); 
+0

使用您提供的答案,但必須用var代替let(不能使用const),因爲webtask.io在嘗試使用let時拋出錯誤。 非常感謝您的幫助! – Tronyx