brad_fresh
brad_fresh

Reputation: 129

Can't form an object dynamically

I encounter a problem while trying to form an object dynamically. errorKey should be a string which again forms dynamically. It can be for example

dispatch(stopSubmit("edit", { 'key': { 'tiny_error': data.messages[0] } }));

The problem is that compiler doesnt't see the variable errorKey at all. When hardcoded like is listed above, the code works as expected. But how to form an appropriate object dynamically?

    const errorMessage = data.messages[0];
    const errorKey = errorMessage.substring(0, errorMessage.length - 1));
    dispatch(stopSubmit("edit", { 'key': { errorKey: data.messages[0] } }));

Upvotes: 0

Views: 29

Answers (1)

Auroratide
Auroratide

Reputation: 2577

I believe the syntax you're looking for is brackets around errorKey, like so:

{ [errorKey]: data.messages[0] }

Upvotes: 3

Related Questions