Today I Learned - Rocky Kev

TIL Computed Properties in Javascript

POSTED ON:

TAGS:

Today I learned about Computed Property names.

This is useful for a lot of reasons, like creating a Factory Pattern where you need to shape objects with unique keys.

const yourKeyVariable = "happyCount";
const someValueArray= [...];

const obj = {
[yourKeyVariable]: someValueArray,
}

another example:

// Computed property names
let i = 0;
const a = {
[`foo${++i}`]: i,
[`foo${++i}`]: i,
[`foo${++i}`]: i,
};

MDN on Computed Property Names

and via StackOverflow


Related TILs

Tagged:

TIL ways to organize eventListeners

Some code spaghetti I made recently was creating a lot of eventListeners for multiple buttons, and then forgot what they all connected to.

TIL Computed Properties in Javascript

Today I learned about Computed Property names.

TIL Computed Properties in Javascript

Today I learned about Computed Property names.