1

js数组切片最优解,js一维数组转二维数组一步到位

typescript版本
function arrReduceWidthNumber(arr: any[], count: number){
    return arr.reduce((sum: any[], item: any, index: number, arr:string[])=>{
        const curStep = arr.slice(sum.length * count, (sum.length + 1) * count);
        if(!!curStep.length){
            sum.push(curStep);
        }
        return sum;
    },[])
}
javascript版本
function arrReduceWidthNumber(arr, count){
    return arr.reduce((sum, item, index, arr)=>{
        const curStep = arr.slice(sum.length * count, (sum.length + 1) * count);
        if(!!curStep.length){
            sum.push(curStep);
        }
        return sum;
    },[])
}

demo(本人试用许久,至今未发现任何副作用,请放心服用)

image.png


web525
157 声望4 粉丝