The following code in JavaScript:
const mixedObjects = [1,2,3].map(num => {
return { a: 'always same' }
});
returns the following result:
[
{a: 'always same'},
{a: 'always same'},
{a: 'always same'}
]
I want to return the following without declaring an empty array outside and then pushing into it. So do it all inside the map. Is this possible?
What I want to return:
[
{a: 'always same'},
{b: 1},
{a: 'always same'},
{b: 2},
{a: 'always same'},
{b: 3}
]
[1,2,3].reduce((result, b) => [...result, { a: 'always same' }, { b }] , []);