মনে করুন আমাদের একটি স্ট্রিং আছে যেটিতে শব্দগুলো হাইফন দিয়ে সেপারেটেড −
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