ts 将100个元素,每行显示9个元素,然后显示出所有行的元素,由此我们延伸出一个项目需求的简单算法实现。

1、先看一下baidu ai出的结果:

2、我们将上面的代码修改下,定义一个数组,然后记录每行的行号及相应的元素:

复制代码
<template>
  <div>console</div>
</template>
<script setup lang="ts">
import { onMounted, reactive } from "vue";
const list = reactive([] as any[]);
function printRows(elementsCount: number, elementsPerRow: number): void {
  const rowsCount = Math.ceil(elementsCount / elementsPerRow);
  for (let i = 0; i < rowsCount; i++) {
    let row = "";
    for (let j = 0; j < elementsPerRow; j++) {
      const elementIndex = i * elementsPerRow + j;
      if (elementIndex < elementsCount) {
        row += `${elementIndex}, `;
      } else {
        break; // 超出元素总数,退出内层循环
      }
    }
    list.push({
      row: i,
      rowElements: row,
    });
    // console.log(row.trim().replace(/, $/, "")); // 移除末尾的逗号并打印
  }
  for(let i = 0; i < list.length; i++){
    console.log(list[i].row, list[i].rowElements)
  }
}

onMounted(() => {
  printRows(100, 9);
});
</script>

3、我们再将上面的代码优化下,让其存对象。

代码:

复制代码
<template>
  <div>console</div>
</template>
<script setup lang="ts">
import { onMounted, reactive } from "vue";
const list = reactive([] as any[]);
function printRows(sourceList, elementsPerRow: number) {
  let elementsCount = sourceList.length;
  const rowsCount = Math.ceil(elementsCount / elementsPerRow);
  for (let i = 0; i < rowsCount; i++) {
    // let row = "";
    let rowList = [];
    for (let j = 0; j < elementsPerRow; j++) {
      const elementIndex = i * elementsPerRow + j;
      if (elementIndex < elementsCount) {
        rowList.push(sourceList[elementIndex]);
      } else {
        break; // 超出元素总数,退出内层循环
      }
    }
    list.push({
      row: i,
      rowElements: rowList,
    });
    // console.log(row.trim().replace(/, $/, "")); // 移除末尾的逗号并打印
  }
  for (let i = 0; i < list.length; i++) {
    console.log(list[i].row, list[i].rowElements);
  }
}

onMounted(() => {
  printRows(
    [
      {
        id: 1,
        name: "a",
      },
      {
        id: 2,
        name: "b",
      },
      {
        id: 3,
        name: "c",
      },
      { id: 4, name: "d" },
      {
        id: 5,
        name: "e",
      },
      {
        id: 6,
        name: "f",
      },
      {
        id: 7,
        name: "g",
      },
      {
        id: 8,
        name: "h",
      },
      {
        id: 9,
        name: "i",
      },
      {
        id: 10,
        name: "j",
      },
      {
        id: 11,
        name: "k",
      },
      {
        id: 12,
        name: "l",
      },
      {
        id: 13,
        name: "m",
      },
      {
        id: 14,
        name: "n",
      },
      {
        id: 15,
        name: "o",
      },
      {
        id: 16,
        name: "p",
      },
      {
        id: 17,
        name: "q",
      },
    ],
    9
  );
});
</script>

4、最后,项目有这样的需求:用户输入一串字符串,然后我们要根据串的情况,进行自动填到相应的位置上去,来达到整个canvas的输出,也就是达到输出到指定的位置上,但是用户输入情况比较复杂,所以我们就要根据canvas的宽度与高度来自动进行计算,最好我们就有了上面的处理。

相关推荐
愚者Pro5 小时前
Flutter Widget组件学习(专为 Uniapp 转 Flutter 定制)
vue.js·学习·flutter·uni-app
前端毕业班8 小时前
uniapp web 灵活控制 style scoped
前端·javascript·vue.js
卤蛋fg69 小时前
vxe-table 数据分组 + 单元格图表:柱状图与饼图渲染实战
vue.js
用户8417948145610 小时前
vxe-table 数据分组:三种展示方式详解
vue.js
LJA6484412 小时前
用 MCP + 一句话生成了完整的 CRUD 页面
vue.js
梵得儿SHI13 小时前
Vue 项目实战与性能优化:工程化与协作全指南(规范 + 配置 + 协作 + 文档)
前端·vue.js·代码规范·eslint·团队协作·前端工程化·前端架构
xjf771113 小时前
AI 可读性与识别能力对比-TypeDom vs Vue
前端·vue.js·人工智能
蜡台14 小时前
Vue2 + TS,分路径参数、查询参数、装饰器组件 / Vue.extend 两种写法,同时补充类型约束、监听路由、动态路由取值。
前端·javascript·vue.js·router
用户85748243548014 小时前
useList 通用列表管理hook
vue.js·微信小程序
Ciito16 小时前
Win11 系统运行 node 项目 network: unavailable 问题解决
前端·vue.js