How could I write the following code more functionally using ES6, without any 3rd party libraries?
// sample pager array
// * output up to 11 pages
// * the current page in the middle, if page > 5
// * don't include pager < 1 or pager > lastPage
// * Expected output using example:
// [9,10,11,12,13,14,15,16,17,18,19]
const page = 14 // by example
const lastPage = 40 // by example
const pagerPages = page => {
let newArray = []
for (let i = page - 5; i <= page + 5; i++) {
i >= 1 && i <= lastPage ? newArray.push(i) : null
}
return newArray
}
I would like to avoid Array.push, and possibly the for loop, but I'm not sure how I would achieve it in this situation.
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…