javascript - 什么是分块[chunked.length - 1]?在这段代码中
问题描述
我在搜索 js 中数组块的其他解决方案时发现了这段代码,但我偶然发现了这部分:const last = chunked[chunked.length - 1];
?有人可以解释它的作用吗?
function chunk(array, size) {
const chunked = [];
for (let element of array) {
const last = chunked[chunked.length - 1];
if (!last || last.length === size) {
chunked.push([element]);
} else {
last.push(element);
}
}
return chunked;
}
chunk([1, 2, 3, 4, 5, 6, 7, 8], 3)
result:
[[ 1, 2, 3], [4, 5, 6], [7, 8]]
解决方案
推荐阅读
- python - 使用 scikit_learn 中的 inverse_transform MinMaxScaler 强制数据帧位于另一个数据帧的范围内
- html - 在 html 标签中添加视口特定的样式
- c# - 如何在设计时为 xaml 选择默认资源字典?
- alexa - 我能否确定 Alexa 请求是由例程还是用户触发的?
- javascript - Puppeteer - 在 page.select() 之后等待网络请求完成
- elasticsearch - 弹性查询布尔必须匹配问题
- sql - SQLite 查找带有两个“a”字母的单词
- sql - 在文本字符串中搜索特定单词 - SQL
- modelica - 新 Dymola UI 中的变量菜单在哪里?
- r - 如何删除 data.table 的不均匀列中的重复值?