S
S
Spawner2017-08-20 08:40:28
JavaScript
Spawner, 2017-08-20 08:40:28

How to put a value from another array into the key name of an associative array?

I have a script that dynamically adds the received values ​​​​to an associative array, the array itself looks something like this:

var object = {
    _id: chatMsg['user_id'],
    chatlog: {
        msg: {
            attachments: chatMsg['attachments'],
            date: chatMsg['date'],
            msg_id: chatMsg['message_id']
        }
    }
}

And I need to place the value from chatMsg['message_id'] instead of the key name "msg".
Approximately like this:
var object = {
    _id: chatMsg['user_id'],
    chatlog: {
        chatMsg['message_id']: {
            attachments: chatMsg['attachments'],
            date: chatMsg['date']
        }
    }
}

When I try to do what I need, the node swears at a syntax error. It is clear.
How would you recommend solving the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
N
Negwereth, 2017-08-20
@Spawner

var object = {
    _id: chatMsg['user_id'],
    chatlog: {
        [chatMsg['message_id']]: {
            attachments: chatMsg['attachments'],
            date: chatMsg['date']
        }
    }
}

And this is not PHP here, arrays are different with us.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question