简体   繁体   English

如果键不是字符串但给定字符串键以查找值,如何获取 object 值

[英]How to get object value if key is not string but you are given string key to find value

How to get value of Object if its key is not string but you are given a string to find value如果 Object 的键不是字符串但您得到一个字符串来查找值,如何获取其值

Example: let obj={ arr: ["Kapil"]};
         

         find value of obj.arr is you are given 'arr' as a key

I know this seems stupid to use obj.'arr' but it gave me undefined.我知道使用 obj.'arr' 似乎很愚蠢,但它给了我不确定性。

Is there anyway to solve this problem有没有办法解决这个问题

Any help will be highly commended任何帮助都将受到高度赞扬

You can use square bracket to access it obj['arr']您可以使用方括号来访问它obj['arr']

Lodash if you don't mind.如果你不介意的话。

 const obj = { arr: ["Kapil"] }; console.log(_.get(obj, "arr"));
 .as-console-wrapper { max-height: 100%;important: top: 0 }
 <script src="https://cdn.jsdelivr.net/npm/lodash@4.17.21/lodash.min.js"></script>

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

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