我正在努力在reactJS中创建自定义的滑动块,我正在为此创建一个函数来生成预期的输出,该函数需要sliderItems(保存数组列表)、slider count(变量名"spliceCount")以及应该包含多少项slide(变量名"slideBy ').
假设我有一个函数,它接受像array, splice-count and slideBy这样的三个属性,并根据拼接计数和滑动返回给我数组的子集. 到目前为止,通过下面的函数,我能够生成如下片段所示的输出.
const list = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K'];
const spliceCount = 3;
const slideBy = 2;
function getSusetofArray(array, splice, slide) {
const arrayList = [...array];
const subsetList = [];
while(arrayList.length > 0) {
subsetList.push(arrayList.splice(0, splice));
}
return subsetList;
}
console.log(getSusetofArray(list, spliceCount, slideBy));
// current output with above function
[['A', 'B', 'C'], ['D', 'E', 'F'], ['G', 'H', 'I'], ['J', 'K']]
然而,在spliceCount和slideBy的帮助下,期望值需要像下面一个一样
expected output = [['A', 'B', 'C'], ['C', 'D', 'E'], ['E', 'F', 'G'], ['G', 'H', 'I'], ['I', 'J', 'K'], ['K']]