This question already has an answer here:
Closed 11 years ago.
Possible Duplicate:
Javascript Objects: Dynamic variable names?
I have a json string passed in from a cgi script. This json string has a list of id's. In javascript
var informationObj = jQuery.parseJSON(information);
tid = informationObj.idList[0].id;
tid is now an ID and I want to use it to access objects within the json string itself like so:
alert (informationObj.tid.rpath);
However this does not seem to work. I have also tried:
alert (informationObj.eval(tid).rpath);
Is there a way around this?
Thanks.
You need this form:
informationObj[tid].rpath
They are equivalent:
var a = 'something';
b[a] === b.something
Use bracket notation:
alert(informationObj[tid].rpath);
Related
This question already has answers here:
What's the best way to convert a number to a string in JavaScript?
(25 answers)
Closed 1 year ago.
As you know, if I want to the get numeric value of a variable (e.g. $event) I'd do:
<ax-text-box (valueChange)='documentSearchItem.fromPrice=(+$event)'></ax-text-box>
I add a + to do that.
Is there a simple way like this, to get the string value of a variable ?
p.s. I don't want to use a method. I want to be able to use it in HTML templates.
let v = 2 // whatever value
let s = v.toString()
Lots of ways. A short one without methods is using template strings:
let inputNum = 222;
let newString = `${inputNum}`;
console.log(typeof(newString));
This question already has answers here:
How to check if object property exists with a variable holding the property name?
(11 answers)
How to use a variable for a key in a JavaScript object literal?
(16 answers)
Closed 5 years ago.
I'm attempting to run a list of strings through an object. When I do it individually it works, but when I pass it through as a string it doesn't work. How would I fix this?
// this doesn't work
var a = "IntegrationItem1";
var data = faq.a;
// but this works
var data = faq.IntegrationItem1;
What's causing the first example to not work? Is the variable data seeing it as faq."IntegrationItem1" instead of faq.IntegrationItem1?
You can access properties of the object using it's names:
var a = "IntegrationItem1";
var data = faq[a];
what you need is faq["IntegrationItem1"] => faq[a]
This question already has answers here:
Use dynamic variable names in JavaScript
(19 answers)
Closed 7 years ago.
I have a question that I could not find the answer, or perhaps cannot phrase the way it should...
I would like to trick javascript's way of handling variables...
Let's say in php I could do something like:
$test['usr_'.$id]=826
But when I try to do the same in Javascript/jQuery:
$("#usr_rank_h").val('rank_'+id);
It will output rank_826 instead of the value of the var rank_826
The equivalent idiom in javascript is actually
var id = 826;
var test = {};
test['rank_'+id] = 826;
Which gives you back an object of the form
{
'rank_826': 826
}
PS: I'm not sure why you are using jQuery in this case, are you getting the id from an input ?
This question already has answers here:
JavaScript object: access variable property by name as string [duplicate]
(3 answers)
Closed 8 years ago.
Please explain, why this code it's not allowed in javascript and how to make it.
var p = "inputText";
regError.p
This will give me undefined but
regError.inputText
will give me a correct result.
You can do it by using bracket notation:
regError[p]
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Property_Accessors
If you have an object like this
var regError = {
inputText : 'something'
}
and you want to access it with a variable, you'll have to use bracket notation
var p = "inputText";
var result = regError[p]; // returns "something"
Use with bracket notation:
regError[p]
You can check the difference between them here and there
This question already has answers here:
"Variable" variables in JavaScript
(9 answers)
Closed 9 years ago.
I can't figure out how to use the name of a variable previously created with eval, without knowing it.
I mean:
function getName(menu_name, level){
eval("var menu_"+level+"="+menu_name);
}
Now how do I get the name of the variable I just created? Probably keep using eval, but I have to put that name into a $.post call as one of my field name.
Thanks in advice.
If level is an integer, you can treat it as a numerical index for an array:
var menu = [];
menu[level] = menu_name;
If level is anything else, you can treat it as a key for a dictionary/associative array:
var menu = {};
menu[level] = menu_name;
Then, for either of the solutions, if you want to access your menu_name, simply call menu[level].