Adding string to object when iterating through object keys in javascript [duplicate]












0
















This question already has an answer here:




  • JavaScript property access: dot notation vs. brackets?

    11 answers



  • Dynamically access object property using variable

    11 answers




I have the following function in javascript that adds div DOM objects to an Interface object called pageDivs.



FCInterface.prototype.initializePageDivs = function() {
this.pageDivs.body = document.getElementsByTagName('body')[0];
this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


The goal here is when this method is called, I have an object self.pageDivs with a bunch of object key to element list of DOM items I can use later in my application. But instead of manually calling each one by one, I think it would be smarter to add these to an object and iterate over them when adding the DOM objects. Something like



FCInterface.prototype.initializePageDivs = function() {
var d = {
body:document.getElementsByTagName('body')[0],
mainMenu:document.getElementsByClassName('mainMenu')[0],
avatarMenu:document.getElementsByClassName('avatarMenu')[0]
},
self = this;

for (var k in d) {
var dd = 'self.pageDivs.'+k;
dd = d[k];
}
// this.pageDivs.body = document.getElementsByTagName('body')[0];
// this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
// this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


But I am having an issue trying to add the string self.pageDivs. to the element k in my iteration. When I do 'self.pageDivs.'+k it is not working. What am I missing?










share|improve this question













marked as duplicate by Satpal javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 7:27


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.



















  • Use Bracket notation i.e. self.pageDivs[k] = d[k]; or self["pageDivs"][k] = d[k];

    – Satpal
    Nov 26 '18 at 7:17













  • that is what i was missing! add this as an answer so that i can accept it

    – Zach Smith
    Nov 26 '18 at 7:23
















0
















This question already has an answer here:




  • JavaScript property access: dot notation vs. brackets?

    11 answers



  • Dynamically access object property using variable

    11 answers




I have the following function in javascript that adds div DOM objects to an Interface object called pageDivs.



FCInterface.prototype.initializePageDivs = function() {
this.pageDivs.body = document.getElementsByTagName('body')[0];
this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


The goal here is when this method is called, I have an object self.pageDivs with a bunch of object key to element list of DOM items I can use later in my application. But instead of manually calling each one by one, I think it would be smarter to add these to an object and iterate over them when adding the DOM objects. Something like



FCInterface.prototype.initializePageDivs = function() {
var d = {
body:document.getElementsByTagName('body')[0],
mainMenu:document.getElementsByClassName('mainMenu')[0],
avatarMenu:document.getElementsByClassName('avatarMenu')[0]
},
self = this;

for (var k in d) {
var dd = 'self.pageDivs.'+k;
dd = d[k];
}
// this.pageDivs.body = document.getElementsByTagName('body')[0];
// this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
// this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


But I am having an issue trying to add the string self.pageDivs. to the element k in my iteration. When I do 'self.pageDivs.'+k it is not working. What am I missing?










share|improve this question













marked as duplicate by Satpal javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 7:27


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.



















  • Use Bracket notation i.e. self.pageDivs[k] = d[k]; or self["pageDivs"][k] = d[k];

    – Satpal
    Nov 26 '18 at 7:17













  • that is what i was missing! add this as an answer so that i can accept it

    – Zach Smith
    Nov 26 '18 at 7:23














0












0








0









This question already has an answer here:




  • JavaScript property access: dot notation vs. brackets?

    11 answers



  • Dynamically access object property using variable

    11 answers




I have the following function in javascript that adds div DOM objects to an Interface object called pageDivs.



FCInterface.prototype.initializePageDivs = function() {
this.pageDivs.body = document.getElementsByTagName('body')[0];
this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


The goal here is when this method is called, I have an object self.pageDivs with a bunch of object key to element list of DOM items I can use later in my application. But instead of manually calling each one by one, I think it would be smarter to add these to an object and iterate over them when adding the DOM objects. Something like



FCInterface.prototype.initializePageDivs = function() {
var d = {
body:document.getElementsByTagName('body')[0],
mainMenu:document.getElementsByClassName('mainMenu')[0],
avatarMenu:document.getElementsByClassName('avatarMenu')[0]
},
self = this;

for (var k in d) {
var dd = 'self.pageDivs.'+k;
dd = d[k];
}
// this.pageDivs.body = document.getElementsByTagName('body')[0];
// this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
// this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


But I am having an issue trying to add the string self.pageDivs. to the element k in my iteration. When I do 'self.pageDivs.'+k it is not working. What am I missing?










share|improve this question















This question already has an answer here:




  • JavaScript property access: dot notation vs. brackets?

    11 answers



  • Dynamically access object property using variable

    11 answers




I have the following function in javascript that adds div DOM objects to an Interface object called pageDivs.



FCInterface.prototype.initializePageDivs = function() {
this.pageDivs.body = document.getElementsByTagName('body')[0];
this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


The goal here is when this method is called, I have an object self.pageDivs with a bunch of object key to element list of DOM items I can use later in my application. But instead of manually calling each one by one, I think it would be smarter to add these to an object and iterate over them when adding the DOM objects. Something like



FCInterface.prototype.initializePageDivs = function() {
var d = {
body:document.getElementsByTagName('body')[0],
mainMenu:document.getElementsByClassName('mainMenu')[0],
avatarMenu:document.getElementsByClassName('avatarMenu')[0]
},
self = this;

for (var k in d) {
var dd = 'self.pageDivs.'+k;
dd = d[k];
}
// this.pageDivs.body = document.getElementsByTagName('body')[0];
// this.pageDivs.mainMenu = document.getElementsByClassName('mainMenu')[0];
// this.pageDivs.avatarMenu = document.getElementsByClassName('avatarMenu')[0];
};


But I am having an issue trying to add the string self.pageDivs. to the element k in my iteration. When I do 'self.pageDivs.'+k it is not working. What am I missing?





This question already has an answer here:




  • JavaScript property access: dot notation vs. brackets?

    11 answers



  • Dynamically access object property using variable

    11 answers








javascript arrays






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 26 '18 at 7:16









Zach SmithZach Smith

1,6702163103




1,6702163103




marked as duplicate by Satpal javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 7:27


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 Satpal javascript
Users with the  javascript badge can single-handedly close javascript questions as duplicates and reopen them as needed.

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 7:27


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.















  • Use Bracket notation i.e. self.pageDivs[k] = d[k]; or self["pageDivs"][k] = d[k];

    – Satpal
    Nov 26 '18 at 7:17













  • that is what i was missing! add this as an answer so that i can accept it

    – Zach Smith
    Nov 26 '18 at 7:23



















  • Use Bracket notation i.e. self.pageDivs[k] = d[k]; or self["pageDivs"][k] = d[k];

    – Satpal
    Nov 26 '18 at 7:17













  • that is what i was missing! add this as an answer so that i can accept it

    – Zach Smith
    Nov 26 '18 at 7:23

















Use Bracket notation i.e. self.pageDivs[k] = d[k]; or self["pageDivs"][k] = d[k];

– Satpal
Nov 26 '18 at 7:17







Use Bracket notation i.e. self.pageDivs[k] = d[k]; or self["pageDivs"][k] = d[k];

– Satpal
Nov 26 '18 at 7:17















that is what i was missing! add this as an answer so that i can accept it

– Zach Smith
Nov 26 '18 at 7:23





that is what i was missing! add this as an answer so that i can accept it

– Zach Smith
Nov 26 '18 at 7:23












0






active

oldest

votes

















0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes

Popular posts from this blog

Tonle Sap (See)

I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

Guatemaltekische Davis-Cup-Mannschaft