I wanted to ask a simple question about dynamic object properties.
In this example function I made, I noticed that I need square brackets between the cur.name variable but not for cur.age in the return statement. I found that it was something about dynamic object properties but was unsure of when I exactly need square brackets for parameters. Could I ask when I need square brackets?
Here is an example function:
const array = [
{name: 'A', age: 1},
{name: 'B', age: 2},
{name: 'C', age: 3},
];
const example = (array) => {
return array.reduce((acc, cur) => {return {...acc, [cur.name]: cur.age} }, {})
}
console.log(example(array));
//Output that I want is {A: 1, B: 2, C: 3} and it works.