เรามีอาร์เรย์ที่มีค่าสตริงและค่าเท็จบางส่วน
เราจำเป็นต้องเขียนฟังก์ชัน JavaScript ที่ใช้ในอาร์เรย์นี้และส่งกลับสตริงที่สร้างโดยการรวมค่าของอาร์เรย์และละเว้นค่าเท็จ
ตัวอย่าง
รหัสสำหรับสิ่งนี้จะเป็น −
const arr = ["Here", "is", null, "an", undefined, "example", 0, "", "of", "a", null, "sentence"];
const joinArray = arr => {
const sentence = arr.reduce((acc, val) => {
return acc + (val || "");
}, "");
return sentence;
};
console.log(joinArray(arr)); ผลลัพธ์
เอาต์พุตในคอนโซล −
Hereisanexampleofasentence