Created
October 11, 2016 12:43
-
-
Save freewayz/d590339cc720c3bd85fe9661fa5102c9 to your computer and use it in GitHub Desktop.
Iterating over array of object and capitailizing the first leter
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myResponse = {"start_date":["A valid integer is required."], | |
"end_date":["A valid integer is required."], "commite":["Require commite."]} | |
//iterate over the serializer error message | |
for (var prop in myResponse) { | |
var errorValue = myResponse[prop]; | |
//test if the current prop has an _ in it string value | |
var _pattern = /_/; | |
if (_pattern.test(prop)) { | |
//split the string based on the _ and join the string based with a spacke | |
prop = prop.split('_').join(' '); | |
} | |
//capitalize the prop | |
prop = prop.replace(/\b\w/g, function(s) { | |
return s.toUpperCase(); | |
}); | |
var errorMessage = prop.concat(" ", errorValue); | |
console.log(errorMessage); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment