合同小程序前端代码仓库
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

35 lines
1.1 KiB

  1. // @ts-nocheck
  2. /**
  3. *
  4. * @param start
  5. * @param end
  6. * @param step 1
  7. * @param fromRight false
  8. * @returns
  9. */
  10. export function range(start : number, end : number, step : number = 1, fromRight : boolean = false) : number[] {
  11. let index = -1;
  12. // 计算范围的长度
  13. let length = Math.max(Math.ceil((end - start) / step), 0);
  14. // 创建一个长度为 length 的数组
  15. // #ifdef APP-ANDROID
  16. const result = Array.fromNative(new IntArray(length.toInt()));
  17. // #endif
  18. // #ifndef APP-ANDROID
  19. const result = new Array(length);
  20. // #endif
  21. // 使用循环生成数字范围数组
  22. let _start = start
  23. while (length-- > 0) {
  24. // 根据 fromRight 参数决定从左侧还是右侧开始填充数组
  25. result[fromRight ? length : ++index] = _start;
  26. _start += step;
  27. }
  28. return result;
  29. }
  30. // 示例
  31. // console.log(range(0, 5)); // 输出: [0, 1, 2, 3, 4]
  32. // console.log(range(1, 10, 2, true)); // 输出: [9, 7, 5, 3, 1]
  33. // console.log(range(5, 0, -1)); // 输出: [5, 4, 3, 2, 1]