how to set a variable as a property [duplicate] - javascript

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
i'm looking for a way to set a variable as a property (for an element same as a div)
here is the code:
function set_it(element,property){
return element.style.property;
}
console.log(set_it(document.getElementById("div1"),"left"));
at last line i'm trying to get "left" property of "div1" by the function
but I get undefined in console
I know , actually it will look for a property named "property" , which doesn't exist
but how can I do this ?
thanks

function set_it(element,property){
return element.style[property];
}
Like so! You can use variables as object accessors when you use square brackets, not dot notation.

Related

get Object value using Object's key where key is a string [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
JavaScript property access: dot notation vs. brackets?
(17 answers)
Closed last month.
I have one object
my_object = {"first_name":"Harry", "age":"24", "second_name":"snow"};
I can get the first name value using
console.log(my_object.first_name); //Harry
and I am getting Harry . Here everything working fine . But for the below code I am not getting values
var num_1 = "first_name";
console.log(my_object.num_1); //undefined
console.log(my_object+"."+num_1); //[object Object].first_name
Please help to solve this. Why I am not able to get object values using num_1 . I need to get the object values using num_1.

How to access object property as a string in javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Access JSON or JS property using string
(4 answers)
Closed 6 years ago.
I can do var foobar = foo.barto access the bar variable.
How would I do this if I had an arbitrary string that told me what element I needed to access. foo."bar" doesn't work.
I think
foo["bar"]
is the syntax you need

Concat string in $scope angularjs [duplicate]

This question already has answers here:
Variable as the property name in a JavaScript object literal? [duplicate]
(3 answers)
Closed 6 years ago.
I want to pass string name through argument in function and concat in $scope, this is possible?
function test(txt){
$scope.person. + txt; // I want to do $scope.person.status; for example
}
Thanks.
Yes, just do:
function test(txt){
$scope.person[txt];
}
Check Property accessors.
As any object you can access their properties using Dot notation or Bracket notation. When you don't know before hand what property you would be accessing you would use Bracket notation ideally.
function test(txt){
$scope.person[txt] = ''; // We need to assign something to it if not it wont work;
}
Demo example : https://jsbin.com/gihizo/3/edit?html,js,console,output

Uncaught SyntaxError: missing ) after argument list / Javascript [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I have custom_comissionData.ProviderID_{parent.customData.serviceLogo}.fix
where parent.customData.serviceLogo is another variable, which value I want to put here as part of variable name.
I mean parent.customData.serviceLogo= 1000
and I want to get value of
custom_comissionData.ProviderID_1000.fix
but the way I typed it causes "Uncaught SyntaxError: missing ) after argument list"
Any way to fix this problem ?
These are equivalent in JavaScript:
object.property
object['property']
You may access your item using the second approach:
custom_comissionData['ProviderID_' + parent.customData.serviceLogo].fix

Use a variable to a variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 8 years ago.
I want to pass a variable into a variable
This is what I want
uk.objects.ARG
I have
var d= ARG
how can I get uk.objects.ARG?
I tried
uk.objects.d
, javascript stores it as the string, not a variable
What should I do in order to make it as a variable ?
I think what you are looking for is perhaps:
var d = "ARG";
uk.objects[d];
You can access JavaScript objects' properties also with the bracket notation:
object[property] = value;
The same works for reading. So in your case you could do the following:
var value = uk.objects[d]

Categories

Resources