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

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

Related

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

Sentence in javascript but variable [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 6 years ago.
i'm working with titanium and alloy.
I need run a JavaScript sentence like
$.D7F15.visible="true";
but D7F15 must be a variable, like
var mifranja="D7F15";
How i can run this??
some help please??
You can use bracket notation to access object property
var mifranja="D7F15";
$[mifranja].visible="true";

How do I get the Value of a specified Key in JavaScript? [duplicate]

This question already has answers here:
Accessing an object property with a dynamically-computed name
(19 answers)
Closed 7 years ago.
I know this question has been asked a million times, I just can't seem to make the answers I find fix my problem.
All I'm trying to do is return the Value of the key that is specified. They Key is in the variable "t":
RefDataTables.forEach(function(t, ti) {
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].t);
});
I know the ".t" is wrong because that is looking for a property (I'm not sure the right word to call it) named "t"
Since "t = "HandleType"" What I'm trying to accomplish is the equivalent of:
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0].HandleType)
You can access it by passing the variable between the square brackets.
response.body = JSON.stringify(RefData_UpdateTracker.response.data[0][t])

Getting object properties that start with a number [duplicate]

This question already has answers here:
Can I get a javascript object property name that starts with a number?
(2 answers)
Closed 8 years ago.
I've got an object where one of the properties starts with a number. I've read that this was allowed, but I'm getting this error:
SyntaxError: identifier starts immediately after numeric literal
var stats = {"LOAD_AVG":{"1_MIN":0.08,"5_MIN":0.08,"15_MIN":0.08},"COUNTS":{"TOTAL":888,"RUNNING":1,"SLEEPING":887,"STOPPED":0,"ZOMBIE":0},"CPU":{"USER_CPU_TIME":8.9,"SYSTEM_CPU_TIME":2.4,"NICE_CPU_TIME":0,"IO_WAIT_TIME":1,"HARD_TIME":0,"SOFT_TIME":0.1,"STEAL_TIME":0},"MEMORY":{"PHYSICAL":{"TOTAL":"3921.98mb","IN_USE":"3682.652mb (93.9%)","AVAILABLE":"239.328mb (6.1%)","BUFFERS":"266.492mb (6.8%)"},"SWAP":{"TOTAL":"4194.296mb","IN_USE":"64.264mb (1.5%)","AVAILABLE":"4130.032mb (98.5%)","CACHE":"1191.328mb (28.4%)"}}};
//works fine
window.alert(stats.COUNTS.TOTAL);
//doesn't work
window.alert(stats.LOAD_AVG.1_MIN);
Here's a fiddle.
How can I access the properties that begin with a number without rewriting the PHP that generated it?
You can use bracket access for properties that aren't valid JavaScript identifiers, that goes for property names with spaces or other language symbols like +, *
window.alert(stats.LOAD_AVG["1_MIN"]);
You can use bracket access anywhere really
window.alert(stats["COUNTS"]["TOTAL"]);

how to set a variable as a property [duplicate]

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.

Categories

Resources