ruby on rails - How can I convert a snakecased hash into camelcase in javascript -
i'm used work javascript connects rails backend , of time 1 of them has budge accept others formatting rules i.e. snake_case ruby , camelcase javascript.
there several methods replace strings between 2 formats, better consider deep replacement of keys in hash in more direct way.
i'm giving method worked out me, in cofeescript, if need javascript version of can convert in coffescript site.
class converter convertstring: (string)-> #switch needed format string.replace(/(\_[a-z])/g, ($1)-> $1.touppercase().replace('_','')) convertobject: (obj)-> newobj = {} k,v of obj val = if typeof v "object" , not array.isarray(v) @camelizeobject(v) else v key = @camelizestring(k) newobj[key] = val newobj
Comments
Post a Comment