मान लीजिए हमारे पास एक स्ट्रिंग है जिसमें शब्द हाइफ़न से अलग हैं:
const str = 'this-is-an-example';
हमें एक JavaScript फ़ंक्शन लिखना है जो ऐसी स्ट्रिंग लेता है और इसे camelCase स्ट्रिंग में बदलता है।
उपरोक्त स्ट्रिंग के लिए आउटपुट होना चाहिए:
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