Skip to content

Instantly share code, notes, and snippets.

@freewayz
Created October 11, 2016 12:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save freewayz/d287d48547112870478286eedc16331d to your computer and use it in GitHub Desktop.
Save freewayz/d287d48547112870478286eedc16331d to your computer and use it in GitHub Desktop.
Iterating over array of object and capitailizing the first leter
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