1

I find numerous guides on how to add variables to existing objects, but nowhere how to add an "existing" variable to an existing object.

I have a whole list with variables already defined in my script. e.g.: a, b, c, d. and they all have their own values. Now I want to call a function on all these variables and then show the variable-names and outcome in console. Therefor I want to create an object out of them to loop through. How do I do this?

This is my workflow: Values are created in various places in the script:

a = 1.333;
b = 1.64252345;
c = 2.980988;

I create the object and try to add the already existing variables (this is where I fail):

var abc = {};
abc.a;
abc.b;
abc.c;

I want to loop through the object, flooring all numbers, and printing the variable-name with the returned number:

var i;
for (i = 0; i < abc.length; ++i) {
    var variablename = Object.keys(abc[i]);
    var flooredvalue = floor(abc[i]);
    var abc[i] = flooredvalue; // Save the floored value back to the variable.
    console.log(variablename+": "+flooredvalue);
}

My desired output in console.log:

a = 1
b = 1
c = 2
7
  • Why not immediately define/use variables within object? Commented Feb 6, 2016 at 8:19
  • There seems to be a lot of misunderstanding here. Objects have properties, the names of properties are not connected to any variables names. Then, floor is a method of Math object, not window. Also, objects don't have length property, you need for..in loop to iterate objects. Commented Feb 6, 2016 at 8:20
  • How can I loop through a list of variables, floor them all, and print the variablename + flooredvalue in the console? Commented Feb 6, 2016 at 8:22
  • How can I loop through a list of variables.. You can't do it with loop unless you place them into object (i.e. make them object properties). Commented Feb 6, 2016 at 8:22
  • @hindmost How can I do it without a loop but very easily to a list of variables? Because I need to do this to around 30 variables... Commented Feb 6, 2016 at 8:24

1 Answer 1

2

Looping through the array of object keys will be a good 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'])
for (i = 0; i < 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);

Sign up to request clarification or add additional context in comments.

4 Comments

when iterating over object, we can use for...in for(key in abc){ console.log(Math.floor(abc[key])); }
Thank you very much. This worked, but only inside the for-loop function. Once I tried console.log(a) outside the loop at the bottom, I get the original value. Can I have the floored value saved to the original variable inside the for-loop?
@Ananthaprakash yes. It is the easiest way, but the thing is that, its supported for IE10+ only. So, I tried to make a more general solution. This supports IE9+
@mesqueeb If you want to change the original variables, then you'll need to do window.a I'll update the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.