cachaito
3/11/2015 - 9:40 AM

Ciekawe podjeście do replace() and slice()

Pytanie zadane na: http://www.reddit.com/r/javascript/comments/2ym834/best_way_to_accomplish_simple_string_modification/ Zamienić w jak najprostszy sposób elementy z przedrostkiem 'user' ('userName' --> 'name')

var elemNames = ['userName', 'userSurname', 'userEmail', 'userPhone'],
    newArray = [];

//lame
elemNames.forEach(function(elem) {
  var tempName = elem.replace('user', '');
  tempName = tempName.charAt(0).toLowerCase() + tempName.slice(1);
  newArray.push(tempName);
});

//nice
elemNames.forEach(function(elem) {
  newArray.push(elem.charAt(4).toLowerCase() + elem.slice(5));
}