javascript - 每次打印结果集在 3 个数据中
问题描述
- 我正在尝试以三个为一组打印数据。
- 所以我想我会迭代数组,每次我都会取三个元素并打印它。
- 所以我想我会使用 slice 但不工作
- 但我不确定如何进行。
- 在下面提供我的代码片段。
- 我调试了但仍然不确定如何进行。
let array = [1, 4, 5, 6, 7, 78, 3, 999, 544, 3, 3, 32233, 223, ];
array.map(search => {
// return {
console.log("chunks of data--->", search.slice(3));
// };
});
解决方案
您必须index
为此使用
let array = [1, 4, 5, 6, 7, 78, 3, 999, 544, 3, 3, 32233, 223, ];
array.map((search,index) => {
if(index%3!==0){
return;
}
let limit = index+3;
// this part need when index almost at the end of the array
if((index+3)>=array.length){
limit =array.length;
}
console.log("chunks of data--->", array.slice(index,limit));
// };
});
推荐阅读
- sql-server - 如何在 MVC 中计算百分比
- python - Python Boto3 错误 - 删除 Amazon S3 对象时出现“预期的字符串或类似字节的对象”
- django - django.core.exceptions.ImproperlyConfigured:不支持 Django 3.0.1
- c# - Dotnet Core - 查找与您所在文件夹相关的文件夹中的所有文件,然后查找最后添加的 csv 文件
- javascript - 如何在超链接点击时用 JavaScript 打开 PDF 文件?
- javascript - 角度 8 和sailsJs 的套接字io 请求错误
- firebase - 如何限制对特定 Firebase 节点的读写
- uiscrollview - 故事板自由形式模式下的视图控制器最大高度
- angular - Twitter 的 Fabric 密钥
- javascript - 在 reactjs 和 nextjs 构造函数中获取参考错误:未定义本地存储