Hey ya'll
Hoping to get some light shed on this JavaScript. (Please forgive any terminology errors on my part, still trying to get a grasp of it)
I have the following code that works:
however if I add:
and add [p3] to the end of the document.getElementById statement, the browser returns “undefined”. If I add + [p3] instead, the return is “widget4param5”
If I drop the let objects and use (dot notation) it works:
My question is why does this occur? I'm guessing the reason for the param5 issue with let is because its seeing the value as a string after adding the +, however adding + [p1][p3] doesn't work either (back to the “undefined”). I “wuda thunk” that adding + [p1][p3] would be the same as adding + myParams.myParams2.param5 but it doesn't seem to be the case.
The learning material I am using just gives examples of how to do it but not the “Why it works”. According to the documentation they are providing, there are 4 ways to access the nested objects but one seems to work with multiples and another doesn't. I've tried searching nested objects but nothing is really explaining the why.
Leaves me with 2 options:
1) I am just misreading something or
2) I'm too much of a rock to understand something basic.
Anyway, thanks for taking a look and keeping the laughter at a low roar.
Hoping to get some light shed on this JavaScript. (Please forgive any terminology errors on my part, still trying to get a grasp of it)
I have the following code that works:
Code:
const myParams = {
param1: "widget1",
param2: "widget2",
myParams2: {
param4: "widget4",
param5: "widget5",
param6: "widget6"
}
}
let p1 = "myParams2";
let p2 = "param4";
document.getElementById("Params").innerHTML = myParams[p1][p2];
however if I add:
let p3 = “param5”
and add [p3] to the end of the document.getElementById statement, the browser returns “undefined”. If I add + [p3] instead, the return is “widget4param5”
If I drop the let objects and use (dot notation) it works:
document.getElementById("Params").innerHTML = myParams.myParams2.param4 + myParams.myParams2.param5;
My question is why does this occur? I'm guessing the reason for the param5 issue with let is because its seeing the value as a string after adding the +, however adding + [p1][p3] doesn't work either (back to the “undefined”). I “wuda thunk” that adding + [p1][p3] would be the same as adding + myParams.myParams2.param5 but it doesn't seem to be the case.
The learning material I am using just gives examples of how to do it but not the “Why it works”. According to the documentation they are providing, there are 4 ways to access the nested objects but one seems to work with multiples and another doesn't. I've tried searching nested objects but nothing is really explaining the why.
Leaves me with 2 options:
1) I am just misreading something or
2) I'm too much of a rock to understand something basic.
Anyway, thanks for taking a look and keeping the laughter at a low roar.