How can I reference a JavaScript object with a variable as identifier? [duplicate]
This question already has an answer here:
Dynamic variable name in loop
2 answers
I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them
var lokVan = lok001;
outputObject.innerHTML = lokVan.x;
outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".
How can I get this to work?
Edit: I solved it. Instead of using
var lokVan = lok001;
which reads lok001 as a string, I had to use this
var lokVan = eval(lok001);
This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string
Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do
van = 003;
lokalen[van].x;
where
var lokalen = {
'001': { floor: 0, x: 1, y: 1 },
'002': { floor: 0, x: 2, y: 2 },
'003': { floor: 0, x: 3, y: 3 },
...
}
javascript cordova javascript-objects
marked as duplicate by T.J. Crowder
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 8:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Dynamic variable name in loop
2 answers
I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them
var lokVan = lok001;
outputObject.innerHTML = lokVan.x;
outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".
How can I get this to work?
Edit: I solved it. Instead of using
var lokVan = lok001;
which reads lok001 as a string, I had to use this
var lokVan = eval(lok001);
This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string
Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do
van = 003;
lokalen[van].x;
where
var lokalen = {
'001': { floor: 0, x: 1, y: 1 },
'002': { floor: 0, x: 2, y: 2 },
'003': { floor: 0, x: 3, y: 3 },
...
}
javascript cordova javascript-objects
marked as duplicate by T.J. Crowder
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 8:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
This question already has an answer here:
Dynamic variable name in loop
2 answers
I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them
var lokVan = lok001;
outputObject.innerHTML = lokVan.x;
outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".
How can I get this to work?
Edit: I solved it. Instead of using
var lokVan = lok001;
which reads lok001 as a string, I had to use this
var lokVan = eval(lok001);
This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string
Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do
van = 003;
lokalen[van].x;
where
var lokalen = {
'001': { floor: 0, x: 1, y: 1 },
'002': { floor: 0, x: 2, y: 2 },
'003': { floor: 0, x: 3, y: 3 },
...
}
javascript cordova javascript-objects
This question already has an answer here:
Dynamic variable name in loop
2 answers
I have a whole bunch of JavaScript objects, to be specific, one per classroom in my school. These have a couple properties each. I'm trying to reference them with a variable as name, like this:
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
This goes on for about 150 classrooms. I'm trying to get the x and y posititon from them
var lokVan = lok001;
outputObject.innerHTML = lokVan.x;
outputObject is defined earlier in the document, but lokVan.x seems to output "undefined".
How can I get this to work?
Edit: I solved it. Instead of using
var lokVan = lok001;
which reads lok001 as a string, I had to use this
var lokVan = eval(lok001);
This reads the object lok001 and transfers it to lokVan, instead of trying to read properties from a string
Edit 2: I've read that eval shouldn't be used, so I've now put all the classrooms into another object, so that I can do
van = 003;
lokalen[van].x;
where
var lokalen = {
'001': { floor: 0, x: 1, y: 1 },
'002': { floor: 0, x: 2, y: 2 },
'003': { floor: 0, x: 3, y: 3 },
...
}
This question already has an answer here:
Dynamic variable name in loop
2 answers
javascript cordova javascript-objects
javascript cordova javascript-objects
edited Nov 26 '18 at 14:19
Bjorn Pijnacker
asked Nov 26 '18 at 8:15
Bjorn PijnackerBjorn Pijnacker
11
11
marked as duplicate by T.J. Crowder
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 8:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
marked as duplicate by T.J. Crowder
StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;
$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');
$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Nov 26 '18 at 8:18
This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
You should use = sign to assing objects to variables.
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
Just checked, I did do this in my code, I just forget to put it in here
– Bjorn Pijnacker
Nov 26 '18 at 8:45
add a comment |
Did you just miss the = between var lok001 and {...};?
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
You should use = sign to assing objects to variables.
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
Just checked, I did do this in my code, I just forget to put it in here
– Bjorn Pijnacker
Nov 26 '18 at 8:45
add a comment |
You should use = sign to assing objects to variables.
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
Just checked, I did do this in my code, I just forget to put it in here
– Bjorn Pijnacker
Nov 26 '18 at 8:45
add a comment |
You should use = sign to assing objects to variables.
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
You should use = sign to assing objects to variables.
var lok001 = {floor: 0, x: 2037, y: 713};
var lok002 = {floor: 0, x: 1759, y: 743};
answered Nov 26 '18 at 8:22
ShizonShizon
12
12
Just checked, I did do this in my code, I just forget to put it in here
– Bjorn Pijnacker
Nov 26 '18 at 8:45
add a comment |
Just checked, I did do this in my code, I just forget to put it in here
– Bjorn Pijnacker
Nov 26 '18 at 8:45
Just checked, I did do this in my code, I just forget to put it in here
– Bjorn Pijnacker
Nov 26 '18 at 8:45
Just checked, I did do this in my code, I just forget to put it in here
– Bjorn Pijnacker
Nov 26 '18 at 8:45
add a comment |
Did you just miss the = between var lok001 and {...};?
add a comment |
Did you just miss the = between var lok001 and {...};?
add a comment |
Did you just miss the = between var lok001 and {...};?
Did you just miss the = between var lok001 and {...};?
answered Nov 26 '18 at 8:22
r0bnetr0bnet
2414
2414
add a comment |
add a comment |