TIL Computed Properties in Javascript
POSTED ON:
TAGS: naming javascript mdn
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: naming