สมมติเรามีสตริงสองตัวที่มีอักขระไม่เรียงลำดับเฉพาะ เราต้องเขียนฟังก์ชันที่รับสตริงทั้งสองและคืนค่าสตริงที่สองที่ปรับปรุงแล้ว ซึ่งลบอักขระทั้งหมดที่มีในสตริงแรกออก
สตริงของเราคือ
const first = "hello world"; const second = "hey there";
ฟังก์ชันของเราสำหรับลบอักขระทั้งหมดของสตริงแรกออกจากสตริงที่สอง
const removeAll = (first, second) => {
const newArr = second.split("").filter(el => {
return !first.includes(el);
});
return newArr.join("");
};
มาเขียนโค้ดสำหรับฟังก์ชันนี้กัน
ตัวอย่าง
const first = "hello world";
const second = "hey there";
const removeAll = (first, second) => {
const newArr = second.split("").filter(el => {
return !first.includes(el);
});
return newArr.join("");
};
console.log(removeAll(first, second));
ผลลัพธ์
ผลลัพธ์ในคอนโซลจะเป็นดังนี้
yt