I've seen plenty of easy ways to convert camelCaseNames
to camel Case Names
, etc. but none on how to convert Sentence case names
to sentenceCaseNames
. Is there any easy way to do this in JS?
This should do the trick :
function toCamelCase(sentenceCase) {
var out = "";
sentenceCase.split(" ").forEach(function (el, idx) {
var add = el.toLowerCase();
out += (idx === 0 ? add : add[0].toUpperCase() + add.slice(1));
});
return out;
}
Explanation:
sentenceCase.split(" ")
creates and array out of the sentence eg. ["Sentence", "case", "names"]
forEach
loops through each variable in the array
inside the loop each string is lowercased, then the first letter is uppercased(apart for the first string) and the new string is appended to the out
variable which is what the function will eventually return as the result.