您当前的位置: 首页 >  Java

暂无认证

  • 0浏览

    0关注

    92582博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

JavaScript分片数值、分段、切割、Number、push、Math、round

发布时间:2022-05-14 21:10:20 ,浏览量:0

目录
  • 1、分片大小固定
  • 2、分片次数固定
1、分片大小固定
function fixedSize(params = 700, fixedSize = 100) { params = Number(params); fixedSize = Number(fixedSize) if (!params || !fixedSize) return '数据有误'; let arrayObjec = [], start = 0; for (let i = 0; i < params; i++) { if (i - start == fixedSize) (arrayObjec.push({ start: start, end: i }), start = i + 1); } let len = arrayObjec.length; if (arrayObjec[len - 1].end < params) arrayObjec.push({ start: arrayObjec[len - 1].end + 1, end: params, }); return arrayObjec; } console.log(fixedSize(10089, 1000)); // (11) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 
2、分片次数固定
function fixedTimes(params = 700, fixedTimes = 7) { params = Number(params); fixedTimes = Number(fixedTimes) if (!params || !fixedTimes) throw new Error('数据有误'); let arrayObjec = [], val = Math.round(params / fixedTimes), start = 0; for (let i = 0; i < fixedTimes; i++) (arrayObjec.push({ start: start, end: start + val }), start = start + val + 1); arrayObjec[arrayObjec.length - 1].end = params; return arrayObjec; } console.log(fixedTimes(7368, 12)); // (12) [{…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}, {…}] 
关注
打赏
1653961664
查看更多评论
立即登录/注册

微信扫码登录

0.3865s