Trying to smooth out the first character in an array of strings, why doesn't this work?

I am new to Javascript. I am trying to write a function that converts, for example, list-style-image to listStyleImage .

I came up with a function, but it doesn't seem to work. Can someone point me to the problem here?

 var myStr = "list-style-image"; function camelize(str){ var newStr = ""; var newArr = []; if(str.indexOf("-") != -1){ newArr = str.split("-"); for(var i = 1 ; i < newArr.length ; i++){ newArr[i].charAt(0).toUpperCase(); } newStr = newArr.join(""); } return newStr; } console.log(camelize(myStr)); 
+9
source share
11 answers

In fact, you have to reassign the array element:

  for(var i = 1 ; i < newArr.length ; i++){ newArr[i] = newArr[i].charAt(0).toUpperCase(); } 

The "toUpperCase ()" function returns a new line, but does not change the original.

You can check to make sure newArr[i] is an empty string if you get an input string with two consecutive dashes.

edit - noticed that SO contributor @lonesomeday correctly indicates that you also need to paste the rest of each line again:

  newArr[i] = newArr[i].charAt(0).toUpperCase() + newArr[i].substr(1); 
+16
source

In your for loop, you need to replace the value of newArr[i] instead of just calculating it:

 for(var i = 1 ; i < newArr.length ; i++){ newArr[i] = newArr[i].charAt(0).toUpperCase() + newArr[i].substr(1); } 
+6
source

Here is my solution with ES6. This is an example in which I store the days of the week in my array and write them in uppercase using the for... of loop.

 const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; for (let day of days) { day = day.charAt(0).toUpperCase() + day.substr(1); console.log(day); } 

Here is the documentation link: for ... cyclic documentation

+5
source

You don't need an array to replace the hyphen and lowercase uppercase letter -

 function camelCase(s){ var rx= /\-([az])/g; if(s=== s.toUpperCase()) s= s.toLowerCase(); return s.replace(rx, function(a, b){ return b.toUpperCase(); }); } camelCase("list-style-image") /* returned value: (String) listStyleImage */ 
+2
source

you need to save the capital letter in the array. Please refer to the modified cycle below,

 for(var i = 1 ; i < newArr.length ; i++) { newArr[i] = newArr[i].charAt(0).toUpperCase() + newArr[i].substr(1,newArr[i].length-1); } 
+2
source

A little longer, but it does the job, mostly playing with arrays:

 function titleCase(str) { var arr = []; var arr2 = []; var strLower = ""; var strLower2 = ""; var i; arr = str.split(' '); for (i=0; i < arr.length; i++) { arr[i] = arr[i].toLowerCase(); strLower = arr[i]; arr2 = strLower.split(''); arr2[0] = arr2[0].toUpperCase(); strLower2 = arr2.join(''); arr[i] = strLower2; } str = arr.join(' '); return str; } titleCase("I'm a little tea pot"); 
+2
source

The substr() method returns the portion of the string between the starting index and the number of characters after it. Source: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/substr

 const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; for (let day of days) { day = day.substr(0, 1).toUpperCase() + day.substr(1); console.log(day); } 
+2
source
 const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; days.map( a => a.charAt(0).toUpperCase() + a.substr(1) ); 
+2
source

Starting with JavaScript ES6, you can achieve a β€œcamel” array of strings in a single line:

 let newArrCamel= newArr.map(item=> item.charAt(0).toUpperCase() + item.substr(1).toLowerCase()) 
+2
source
 function titleCase(str){ // my codelooks like Jmorazano but i only use 4 var. var array1 = []; var array2 = []; var array3 = ""; var i; array1 = str.split(" "); for (i=0; i < array1.length; i++) { array1[i]= array1[i].toLowerCase(); array2=array1[i].split(""); array2[0]=array2[0].toUpperCase(); array3 = array2.join(""); array1[i] = array3;} str = array1.join(' '); return str;} titleCase("I AM WhO i aM"); //Output:I Am Who I Am // my 1st post. 
0
source

Here's another solution, although I'm so late in the game.

 const days = ['sunday', 'monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday']; let capitalize = days.map(day => day.charAt(0).toUpperCase() + day.slice(1).toLowerCase()); console.log(capitalize); 

0
source

All Articles