मान लीजिए, हमारे पास एक स्ट्रिंग है जिसमें इस तरह के हाइफ़न द्वारा अलग किए गए शब्द हैं -
const str = 'this-is-an-example';
हमें एक जावास्क्रिप्ट फ़ंक्शन लिखने की आवश्यकता है जो ऐसी एक स्ट्रिंग लेता है और इसे कैमलकेस स्ट्रिंग में परिवर्तित करता है।
उपरोक्त स्ट्रिंग के लिए, आउटपुट होना चाहिए -
const output = 'thisIsAnExample';
इसके लिए कोड होगा -
const str = 'this-is-an-example';
const changeToCamel = str => {
let newStr = '';
newStr = str
.split('-')
.map((el, ind) => {
return ind && el.length ? el[0].toUpperCase() + el.substring(1)
: el;
})
.join('');
return newStr;
};
console.log(changeToCamel(str)); कंसोल पर आउटपुट निम्न है -
thisIsAnExample