हमारे पास एक ऐरे है जिसमें कुछ स्ट्रिंग मान और कुछ नलिश मान हैं।
हमें एक JavaScript फंक्शन लिखना है जो इस ऐरे को लेता है और ऐरे के मानों को जोड़कर एक स्ट्रिंग बनाता है, नलिश मानों को छोड़कर।
निम्नलिखित हमारा ऐरे है, कुछ null और undefined मानों के साथ -
const arr = ["Here", "is", null, "an", undefined, "example", 0, "", "of", "a", null, "sentence"];
उदाहरण
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