简体   繁体   English

从 Json Object 中提取嵌套值是否有更好的选择

[英]Is there a better alternative to extract nested value from Json Object

I extract generically val2 like below, it works, but is there a lighter way?我像下面一般提取 val2 ,它可以工作,但是有没有更轻松的方法?

let myJsonObject = {
    'key1': {'key1.1': 'value1.1'},
}
let val1 = myJsonObject['key1'];
let key = Object.keys(val1)[0];
let val2 = val1[key];

myJsonObject['key1']['key1.1'] gives you the same result, but this by itself doesn't go anywhere. myJsonObject['key1']['key1.1']给你相同的结果,但这本身不会 go 任何地方。

It would be a lot more interesting and useful for you if you could explain the bigger picture of why you need to read a nested field in your object so that we can discuss about a more general and meaningful solution.如果您能更全面地解释为什么需要在 object 中读取嵌套字段,这对您来说会更有趣和有用,这样我们就可以讨论更通用和更有意义的解决方案。

Some info could be where this object comes from and what possible values/formats it can get.一些信息可能是这个 object 的来源以及它可以获得的可能值/格式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM