Skip to content

Instantly share code, notes, and snippets.

@freewayz
Created October 11, 2016 12:43
Show Gist options
  • Save freewayz/d287d48547112870478286eedc16331d to your computer and use it in GitHub Desktop.
Save freewayz/d287d48547112870478286eedc16331d to your computer and use it in GitHub Desktop.

Revisions

  1. freewayz created this gist Oct 11, 2016.
    20 changes: 20 additions & 0 deletions capitalize-obj.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,20 @@
    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);
    }