This question already has answers here:
How to find JavaScript variable by its name
(7 answers)
Closed 7 years ago.
I saw a lot of answer to call a method from a given object using a string, but no one to get the object itself.
I would like something like that
var a
var b
var c
function getObject(objectAsString)
{
return getMyObject(objectAsString);
}
then if I write
var obj=getObject("a")
my result is obj=a
Is there a function "getMyObject"?
Thanks
See following code
<script>
//in one script
var GlobalvarName = 500;
alert(window["GlobalvarName"]); //alert is : 500
</script>
you could do:
var hello = 'Hello World';
this['hello'];
I wouldn't make those variables so global though. here's why
Instead put them inside an object like:
var obj = {
a: 'Hello',
b: 'World'
}
console.log(obj['a'], obj['b']);
Related
This question already has answers here:
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 4 years ago.
I have a series of variables that I would like to pass into an object and I need the left side key to be pulled from a dynamic variable. How would I go about doing this?
Here's an example:
var characteristic = 'color';
var value = 'green';
// Desired JSON output
var object = {
color: 'green'
}
like so:
var characteristic = 'color';
var value = 'green';
var object = {
[characteristic]: value
}
console.log(object);
This question already has answers here:
JavaScript - Why can't I call a variable "name"?
(2 answers)
Closed 5 years ago.
My question is pretty simple. I'm creating two objects. The second object is referencing an object inside the first object.
var me = {
name: {
first: "justin"
}
};
var name = me.name;
console.log(me.name.first); // "justin"
console.log(name.first); // undefined
Why am I getting undefined in my second console log? Shouldn't I get "justin" instead?
You need to use another name. There is a name variable which is global.
var me = {
name: {
first: "justin"
}
};
var anotherName = me.name;
console.log(me.name.first);
console.log(anotherName.first);
This question already has answers here:
how to access object property using variable [duplicate]
(2 answers)
Closed 7 years ago.
I am trying to use a variable to select from an array:
This works:
var myarray = {bricks:3000, studs:500, shingles:400, tiles:700};
function One() {
alert(myarray.bricks);
}
But this does not work:
var myarray = {bricks:3000, studs:500, shingles:400, tiles:700};
var myvalue = "bricks"
function Two() {
alert(myarray.myvalue);
}
How do I do this properly? Here is a fiddle to show what I am trying to accomplish: https://jsfiddle.net/chrislascelles/xhmx7hgc/2/
Use the [] notation.
var myarray = {bricks:3000, studs:500, shingles:400, tiles:700};
function One() {
alert(myarray.bricks);
}
var myvalue = "bricks" //supplied here to make example work
function Two() {
alert(myarray[myvalue]);
}
Demo
The variable is not an array, it's an object.
To access an element from object using variables you should use Bracket Notation like bellow
alert(myarray[myvalue]);
Fiddle
The only thing you are lacking is the syntax. Here is how it works:
function Two() {
alert(myarray[myvalue]);
}
In javascript, it means the same thing to write these two:
var a = {};
a.foo = "hello";
a["bar"] = "world";
a.bar; // world;
a["foo"]; // hello;
This question already has answers here:
How to find JavaScript variable by its name
(7 answers)
Closed 8 years ago.
I'm trying to find a way of using a string to refer to a variable.
Here is an example using jQuery:
var array = [1,2,3];
$('.array').click(function(){
var foo = $(this).attr('class');
return foo[1];
});
I want this to return the number '2' -
but as foo is a string it will return the sub string 'r'.
edit - the answer I was looking for was:
var array = [1,2,3];
$('.array').click(function(){
var foo = $(this).attr('class');
return eval(foo)[1];
});
I don't know if this is quite what you mean, but a Javascript object can do this.:
foo = {}; // create object
foo["string"] = [1,2,3]; // now the list [1,2,3] can be referenced
// by foo.string, or foo["string"]
console.log(foo["string"][1]); // Output with string.
Is that what you mean?
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 7 months ago.
In PHP I can mess around with variable variables and I'm wondering if I can do the same in JavaScript.
I want to create a new object with a property which's name is based on the value of a variable.
if ( obj.name === 'foo' ) {
var data = { foo: value };
}
if ( obj.name === 'bar' ) {
var data = { bar: value };
}
Is there a shorter way of doing this without using eval()? Something like:
var data = { obj.name: value };
Try this:
var data = {};
data[obj.name] = value;
You can read some more about js objects Here.
Objects in JavaScript are simply hash maps. You can access members by indexing with their names. For your problem you can use
var data = {};
data[obj.name] = value;
I've used this to implement a dynamic dispatch mechanism for arithmetic operations on different numerical types as described here.