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 , not 14.0 or 14 of course.
  • for floats without decimal places use !isnan(n) instead of n === number(n) && n % 1 !== 0. match integers. (will match 14.2, 14.0 , 14)
  • if want match floats, printed out floats use: !isnan(n) && n.tostring().indexof('.'). (will match 14.2, 14.0 , not 14)
  • of course traverse $.each(..) also.

Comments

Popular posts from this blog

Hatching array of circles in AutoCAD using c# -

ios - UITEXTFIELD InputView Uipicker not working in swift -

Python Pig Latin Translator -