javascript - Perform an action on all the member of JS object -
i have js object like
object {id: 1, name: "grain", price: 26.0, dm: 2.0}
i want divide float values
2
. appreciated
traverse through object , check float values. divide:
for (var prop in obj) { var n = obj[prop]; if(n === number(n) && n % 1 !== 0) { obj[prop] = n / 2; } }
please note:
- the above match
14.2
, not14.0
or14
of course. - for floats without decimal places use
!isnan(n)
instead ofn === number(n) && n % 1 !== 0
. match integers. (will match14.2
,14.0
,14
) - if want match floats, printed out floats use:
!isnan(n) && n.tostring().indexof('.')
. (will match14.2
,14.0
, not14
) - of course traverse
$.each(..)
also.
Comments
Post a Comment