Map returns Empty statement array

I have this function

function getDaysArray(days: number): number[] {
        return [...new Array(days)].map((_x, i) => {
            return i + 1;
        });
}

which is supposed to return an array of days by month,
so for example If I log getDaysArray(31) I expect to have an array like this

[1,2,...,31]

but instead, I am getting this:

[empty × 31]

Does anyone know what is happening?

>Solution :

you can do something like this

function getDaysArray(days) {
        return Array.from({length: days}).map((_, i) => i + 1)

}

console.log(getDaysArray(31))

Leave a Reply