I cannot seem to find an answer to this and I am confused.
I have the following set up:
var object1 = {
array1: [
{
prop1: val1,
prop2: val2
},
{
prop1: val1,
prop2, val2
}
]
}
My question is, when I add "prop3" I want to refer to prop1 and prop2. I thought I could use this
as in the below example but it doesn't work.
var object1 = {
array1: [
{
prop1: val1,
prop2: val2,
prop3: "I want to include " + this.prop2 + " in this string"
},
{
prop1: val1,
prop2, val2
}
]
}
I tried this
because I'm aware that accessing a property within an object works using the this
keyword.
However this.prop2
evaluates as undefined
. I presume that this
may not be the whole problem. As when I try:
var object1 = {
array1: [
{
prop1: val1,
prop2: val2,
prop3: "I want to include " + object1.array1[0].prop2 + " in this string"
},
{
prop1: val1,
prop2, val2
}
]
}
With this code I get an error "cannot read property array1 of undefined". However when I take it out the string, so the rest of my js runs okay, and console.log(object1.array1[0].prop2
, i get, as expected, val2
.
Thanks