const idx = query.idx;
let page = 1;
if (idx > 19){
page = 2
};
if (idx > 39){
page = 3
};
if (idx > 59){
page = 4
};
console.log(page);
Run Code Online (Sandbox Code Playgroud)
这是我的代码我试图在每19,39,59之后更改页面变量等是否有更好的方法来编写此代码,就好像我有更多索引我需要添加更多ifs
除以20(页面大小)和圆形结果.像这样的东西:
const idx = query.idx;
const page = Math.ceil((idx + 1) / 20)
console.log(page);
Run Code Online (Sandbox Code Playgroud)
几个测试:
[
1,
5,
15,
19,
20,
21,
36,
39,
40,
41,
49,
59,
60,
68,
].forEach(idx => {
console.log(idx, Math.ceil((idx + 1) / 20))
})Run Code Online (Sandbox Code Playgroud)