I have object in this structure:
obj = {
user: { name: 'jeterson' },
title: 'I am a test'
}
I have one key with value: user.name.
I have trying get value like this: obj[key], meaning obj['user.name']. It not works, only works for obj.title.
My object have many values that are also objects, and i want get value like this:
myobject[mykey]
It is possible get value from property object like above ?
You've got multiple solutions to access an element of an object with its keys:
But you can't do it easily with a variable
key = 'user.name'.If you need to use a variable containing the nested-keys, you could create a function.
Updated answer: An amazingly short way to achieve it is to use
.reduce():Old answer: Use a
forto loop through thekeysand reduce the oject:Hope it helps.