Reputation: 21
I have an array, e.g.,
a = [20208, 20209, 202010, 20206]
I need only the last digit(s) (the month) of each number, e.g.,
a = [8, 9, 10, 6]
How do I do that with JavaScript?
Upvotes: 0
Views: 100
Reputation: 2946
Being numeric values, another option is to use the mod operator rather than slicing:
const a = [20208, 20209, 202010, 20206,20218];
console.log(a.map(x => (x%100)>12?x%10:x%100));
Upvotes: 1
Reputation: 1375
If month is at the end of the year in array then simply loop over item and slice item after year, you will get the month.
const a = [20208, 20209, 202010, 20206, 202011]
const t = a.map(item => +`${item}`.slice(4))
console.log(t)
Upvotes: 3