Home > Back-end >  How to get object value if key is not string but you are given string key to find value
How to get object value if key is not string but you are given string key to find value

Time:08-22

How to get value of Object if its key is not string but you are given a string to find value

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.

Is there anyway to solve this problem

Any help will be highly commended

CodePudding user response:

You can use square bracket to access it obj['arr']

CodePudding user response:

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/[email protected]/lodash.min.js"></script>

  • Related