0

In react you can do something like

const obj = { thing: 'a', }

then const { thing } = obj is a shorthand for const thing = obj.thing

is there a way to convert all object key/value pairs at once?

Object.keys(obj).map((key) => { const eval(key) = obj[key]; }

something like that.

Thanks in advance!

1 Answer 1

2

The only way to do it is by destructuring the object, but you will need to know your object properties for that:

const { prop1, prop2, prop3 } = obj;

And by the way your example with ".map" won't work because you are creating a local constants which won't be accessible outside that function.

Sign up to request clarification or add additional context in comments.

1 Comment

To add to this you can destruct an object of an object like so: const { prop1: { prop2 }} = obj; would output prop2.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.