I have been starting to look at javascript objects, and I have found the format that looks like this.
var obj = {
prop1: 5,
obj2: {
prop1: [3, 6, 3],
prop2: 74,
4_3: {
str: "Hello World"
}
}
};
My question is. How do I get the console to say the string?
I have tried something like this:
console.log(obj.obj2[Object.keys(obj.obj2)[2]].str);
But it didn't work.
CodePudding user response:
I'm not change the input this time
var obj = {
prop1: 5,
obj2: {
prop1: [3, 6, 3],
prop2: 74,
4_3: {
str: "Hello World"
}
}
};
console.log(obj.obj2[Object.keys(obj.obj2)[0]].str);
CodePudding user response:
Why not just reference?
var obj = {
prop1: 5,
obj2: {
prop1: [3, 6, 3],
prop2: 74,
4_3: {
str: "Hello World"
},
}
};
console.log(obj.obj2['43'].str);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>
CodePudding user response:
You can do that using Object.values
.
var obj = {
prop1: 5,
obj2: {
prop1: [3, 6, 3],
prop2: 74,
4_3: {
str: "Hello World"
},
}
};
console.log(obj.obj2[4_3].str);
<iframe name="sif2" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>