如何将我的数组分成 discord.js 中的一些部分?
How to separate my array into some parts in discord.js?
我在嵌入中进行分页,我想为每个页面从一个数组中拆分 4 个元素。我该怎么做呢? (当我点击按钮时更新)。在那之前,我只有第一步,即从第一页获取前 4 个元素,其中:
.slice(0, 4)
如何获得下一个,下一个,等等...
您需要定义:
- currentPage 变量(设置为默认值 1)
- pageSize - 一页应该有多少项目
- countPages - 用于计算,在大边四舍五入
我写了3个方法
loadFirstPage()
loadNextPage()
loadPrevPage()
const input = [1,2,3,4,5,6,7,8,9,10,11,12,13];
let currentPage = 1;
let pageSize = 4;
const countPages = Math.ceil(input.length / pageSize);
function loadFirstPage() {
currentPage = 1;
return [...input].splice(0, pageSize)
}
function loadNextPage() {
const nextPage = currentPage + 1;
if (nextPage <= countPages) {
currentPage = nextPage;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
} else {
currentPage = countPages;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
}
}
function loadPrevPage() {
const prevPage = currentPage - 1;
if (prevPage >= 1) {
currentPage = prevPage;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
} else {
currentPage = 1;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
}
}
console.log('output data: ', loadFirstPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
我在嵌入中进行分页,我想为每个页面从一个数组中拆分 4 个元素。我该怎么做呢? (当我点击按钮时更新)。在那之前,我只有第一步,即从第一页获取前 4 个元素,其中:
.slice(0, 4)
如何获得下一个,下一个,等等...
您需要定义:
- currentPage 变量(设置为默认值 1)
- pageSize - 一页应该有多少项目
- countPages - 用于计算,在大边四舍五入
我写了3个方法
loadFirstPage()
loadNextPage()
loadPrevPage()
const input = [1,2,3,4,5,6,7,8,9,10,11,12,13];
let currentPage = 1;
let pageSize = 4;
const countPages = Math.ceil(input.length / pageSize);
function loadFirstPage() {
currentPage = 1;
return [...input].splice(0, pageSize)
}
function loadNextPage() {
const nextPage = currentPage + 1;
if (nextPage <= countPages) {
currentPage = nextPage;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
} else {
currentPage = countPages;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
}
}
function loadPrevPage() {
const prevPage = currentPage - 1;
if (prevPage >= 1) {
currentPage = prevPage;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
} else {
currentPage = 1;
const startIndex = currentPage * pageSize - pageSize;
return [...input].splice(startIndex, pageSize)
}
}
console.log('output data: ', loadFirstPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadNextPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);
console.log('output data: ', loadPrevPage(), 'current page: ', currentPage);