javascript - Simple function and changing of values -


im working in node, think pure javascript question. lets have simple object this:

var simpleobject = {   decimal: ' 314,2351 ',    id: '324fgfdhf34',   mail: 'anders@jejr.se'   something: 'whateever...' } 

now want create simple sanitize function, takes object[property] argument, change it, , save directly. want place function in node module , require it.

right have function (and more...) in module require in app , binds variable name "purify"

exports.tofloat = function(str) {   str = str.replace(',', '.');   str = parsefloat(str);   return str; } 

and in app can write use function:

simpleobject.decimal = purify.tofloat(simpleobject.decimal) 

but able write more shorter... this:

purify.tofloat(simpleobject.decial) 

... , have value changed , saved directly.

question: how change code above can write , have work:

purify.tofloat(simpleobject.decial) 

you can't call want, because when pass parameter simpleobject.decimal lost reference simpleobject. can instead, quite close:

exports.tofloat = function(obj, propname) {   obj[propname] = parsefloat(obj[propname].replace(',', '.')); } // usage: purify.tofloat(simpleobject, 'decimal'); 

Comments

Popular posts from this blog

java.util.scanner - How to read and add only numbers to array from a text file -

rewrite - Trouble with Wordpress multiple custom querystrings -

php - Accessing static methods using newly created $obj or using class Name -