0

Going over this code in github https://github.com/WebDevSimplified/postman-clone, I simply do not understand below portion

function keyValuePairsToObjects(container) {
    const pairs = container.querySelectorAll('[data-key-value-pair]')
    return [...pairs].reduce((data, pair) => {
        const key = pair.querySelector('[data-key]').value
        const value = pair.querySelector('[data-value]').value
        if (key === '') return data
        return { ...data, [key]: value }
    }, {})
}

{...data, [key]: value} Why is key inside of an array?

0

1 Answer 1

3

Key is not an array, this is the syntax for using a variable name as the key, like the obj["prop"] syntax, { ["prop"]: true } is like { prop: true }.

Context for comments:

> { ["prop"]: true }
{ prop: true }
> { prop: true }
{ prop: true }
Sign up to request clarification or add additional context in comments.

8 Comments

thank you.. I was just looking at the doc and it has no information for something like this.. do I just suck at looking at document or is this a common concept from plain javascript?
If this helped, please accept the answer. Also: this is a plain JS feature.
thanks.. I am trying but it says I have to wait for 5 min before I can accept the answer. thank you.
btw, I did accept this answer, however {["prop"]: true} is NOT equal to {prop: true}
It is, unless you mean using ===, no objects are ever equal unless they reference the same object.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.