String Javascript
[/ux_image_box][code lang="javascript"]String.prototype.isContains = function () {
if (arguments.length !== 1) {
return false;
}
const input = this.valueOf();
let flagContain = true;
for (const data of arguments[0]) {
if (!input.includes(data)) {
flagContain = false;
break;
}
}
return flagContain;
};[/code]
จริงๆ function นี้ สามารถทำเป็น function ธรรมดาก็ได้ ไม่จำเป็นต้องทำเป็น method แบบนี้
แต่เพราะว่าอยากทำเท่ๆ แบบคล้ายๆ string.include, string.filter บลาๆ ก็เลยออกมาเป็นท่า string.isContains
ใช้สำหรับเช็คว่า ข้อมูลที่อยู่ใน array ต้องมีอยู่ใน string ทุกตัว
คล้ายๆกับ include แต่ include เชคตัวเดียวก็ผ่าน
แต่ของผมต้องมีทุกตัวถึงผ่านครับ
[code lang="bash"]"RemainQuota-IS20030005-GENERAL-BBL|KBANK".isContains(['BBL','KBANK']); > true "RemainQuota-IS20030005-GENERAL-BBL|KBANK".isContains(['BBL','KBANK','SCB']); > false[/code]