Add an existing var to a new object in JavaScript and loop through them -
i find numerous guides on how add variables existing objects, how add "existing" variable existing object.
i have whole list variables defined in script. e.g.: a, b, c, d. , have own values. want call function on these variables , show variable-names , outcome in console. therefor want create object out of them loop through. how do this?
this workflow: values created in various places in script:
a = 1.333; b = 1.64252345; c = 2.980988;
i create object , try add existing variables (this fail):
var abc = {}; abc.a; abc.b; abc.c;
i want loop through object, flooring numbers, , printing variable-name returned number:
var i; (i = 0; < abc.length; ++i) { var variablename = object.keys(abc[i]); var flooredvalue = floor(abc[i]); var abc[i] = flooredvalue; // save floored value variable. console.log(variablename+": "+flooredvalue); }
my desired output in console.log:
a = 1 b = 1 c = 2
looping through array of object keys
idea.
a = 1.333; b = 1.64252345; c = 2.980988; var abc = {}; abc.a = a; abc.b = b; abc.c = c; var i; var keys = object.keys(abc); console.log(abc['a']) (i = 0; < keys.length; i++) { var key = keys[i]; var flooredvalue = math.floor( abc[key] ); abc[key] = flooredvalue; window[key] = flooredvalue; //global variable change. } console.log(a); console.log(b); console.log(c);
Comments
Post a Comment