admin管理员组文章数量:1024592
I have a nested object of which I do not know the structure of. For example:
const nestedObject = {
"one": {
"two": {
"three": 3
}
}
}
I wanted to display value of three
.
I have an array like this so that I know how to navigate the object in order to get to three
:
const keys = ["one", "two", "three"]
This doesn't have to be structured this way.
So how do I access one.two.three
given the keys
array above? Or some other data structure. I was thinking of doing a recursion, but it seems too plex and I feel like there is a simple solution out there.
I have a nested object of which I do not know the structure of. For example:
const nestedObject = {
"one": {
"two": {
"three": 3
}
}
}
I wanted to display value of three
.
I have an array like this so that I know how to navigate the object in order to get to three
:
const keys = ["one", "two", "three"]
This doesn't have to be structured this way.
So how do I access one.two.three
given the keys
array above? Or some other data structure. I was thinking of doing a recursion, but it seems too plex and I feel like there is a simple solution out there.
- Please post the JavaScript in a minimal reproducible example. If you haven't actually tried anything yet...do so. – zer00ne Commented Apr 15, 2019 at 9:21
- 1 Possible duplicate of Convert JavaScript string in dot notation into an object reference – Kévin Bibollet Commented Apr 15, 2019 at 9:23
2 Answers
Reset to default 11You can do it with a simple Array.prototype.reduce()
function:
const data = {
"one": {
"two": {
"three": 3
}
}
};
const keys = ["one", "two", "three"];
const value = keys.reduce((a, v) => a[v], data);
console.log(value);
You can use MyObject["fieldName"]
to access the sub object. And then, you can use a recursive function that will use the array containing the indexes to roam through the object
let MyObj = {
"one": {
"two": {
"three": 3
}
}
};
let Keys = ["one", "two", "three"];
function getValue(obj, arrIndexes, index = 0)
{
let currentIndex = arrIndexes[index];
let currentObject = obj[currentIndex];
if (index < arrIndexes.length - 1)
{
return getValue(currentObject, arrIndexes, index + 1)
}
else
{
return currentObject;
}
}
console.log(getValue(MyObj, Keys));
I have a nested object of which I do not know the structure of. For example:
const nestedObject = {
"one": {
"two": {
"three": 3
}
}
}
I wanted to display value of three
.
I have an array like this so that I know how to navigate the object in order to get to three
:
const keys = ["one", "two", "three"]
This doesn't have to be structured this way.
So how do I access one.two.three
given the keys
array above? Or some other data structure. I was thinking of doing a recursion, but it seems too plex and I feel like there is a simple solution out there.
I have a nested object of which I do not know the structure of. For example:
const nestedObject = {
"one": {
"two": {
"three": 3
}
}
}
I wanted to display value of three
.
I have an array like this so that I know how to navigate the object in order to get to three
:
const keys = ["one", "two", "three"]
This doesn't have to be structured this way.
So how do I access one.two.three
given the keys
array above? Or some other data structure. I was thinking of doing a recursion, but it seems too plex and I feel like there is a simple solution out there.
- Please post the JavaScript in a minimal reproducible example. If you haven't actually tried anything yet...do so. – zer00ne Commented Apr 15, 2019 at 9:21
- 1 Possible duplicate of Convert JavaScript string in dot notation into an object reference – Kévin Bibollet Commented Apr 15, 2019 at 9:23
2 Answers
Reset to default 11You can do it with a simple Array.prototype.reduce()
function:
const data = {
"one": {
"two": {
"three": 3
}
}
};
const keys = ["one", "two", "three"];
const value = keys.reduce((a, v) => a[v], data);
console.log(value);
You can use MyObject["fieldName"]
to access the sub object. And then, you can use a recursive function that will use the array containing the indexes to roam through the object
let MyObj = {
"one": {
"two": {
"three": 3
}
}
};
let Keys = ["one", "two", "three"];
function getValue(obj, arrIndexes, index = 0)
{
let currentIndex = arrIndexes[index];
let currentObject = obj[currentIndex];
if (index < arrIndexes.length - 1)
{
return getValue(currentObject, arrIndexes, index + 1)
}
else
{
return currentObject;
}
}
console.log(getValue(MyObj, Keys));
本文标签: javascriptHow to access property of nested object given dynamic keysStack Overflow
版权声明:本文标题:javascript - How to access property of nested object given dynamic keys? - Stack Overflow 内容由热心网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://it.en369.cn/questions/1745528922a2154659.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论