Hi, i cannot make string ‘KOMMUNENUM’ be assigned as a variable in tt2:
var tt2 = 'KOMMUNENUM';
for (i = 0; i < diam.features.length; i++) {
alert(diam.features[i].properties.tt2);
what i really want as a result is this:
alert(diam.features[i].properties.KOMMUNENUM)
is there a way for tt2 to apply KOMMUNENUM?
appreciate any help
Hi,
What is diam
and features
and properties
?
Could you provide a full code snippet?
Hi, i cannot make string ‘KOMMUNENUM’ be assigned as a variable in tt2:
From the code you’ve provided us I see that you have successfully reached what you want, because if you execute console.log(tt2);
the output will be like "KOMMUNENUM"
. Even if you alert(tt2);
you will see this:
Hi, thanks for your answer.
I can try to rephrase the question.
typing this alone gives me the wanted results: alert(diam.features[i].properties.KOMMUNENUM)
but I want to be able to do it dynamically through a variable. Meaning instead of typing KOMMUNENUM (which is a property in an object) i want to be able to just type:
alert(diam.features[i].properties.tt2)
where tt2 can be changed by user in an interface to reference any property name in the object.
So the question is:
how can I make tt2 apply KOMMUNENR as a property in an object?
Send me your object so I can deeper understand what you mean.
For now, I can only suppose that there is no value like diam.features[i].properties.tt2
in your object ot it is but equals some other output.
Doing this var tt2 = 'KOMMUNENUM';
you are not changing the value of diam.features[i].properties.tt2
, instead of it you are creating a new variable which is not related to your object in any way.
Here is a snippet which I hope will help you to understand what are you doing wrong:
var object = {
"first": "output1",
"second": "output2",
"third": {
"firstOf3": "output13",
tt2: "some output"
}
};
console.log(object["third"].tt2); // "some output"
object["third"].tt2 = "KOMMUNENR";
var tt2 = "bla bla bla";
console.log(object["third"].tt2); // "KOMMUNENR"
console.log(tt2); // "bla bla bla"
I’ve used console.log()
instead of alert()
but it do not make much sense in this particular example.
thanks again.
-
tt2 does not exists in the object
-
I’m not trying to add tt2 as a new property to the object
-
I’m not trying change or add a value in the object.
-
The object property i have has several properties (KOMMUNENR, PNR, etc)
-
I want tt2 to reference these based on change, so for example:
user has a dropbox with 2 variables: KOMMUNENR and PNR, when user selects one of those, it will
then run alert(diam.features[i].properties.tt2)
where tt2 is either KOMMUNENR or PNR based on what user picked. So if user picked KOMMUNENR it will run:
alert(diam.features[i].properties.KOMMUNENR)
and if user picked PNR it will run:
alert(diam.features[i].properties.PNR)
In your code features[i]
does what you want.
The trick is to use bracket notation to get the property instead of dot notation.
Thank you very much, this worked 100%.
final result was:
alert(diam.features[i].properties[tt2])
i was using dot notation which was wrong.
1 Like