按要上传的文件大小对文件数组进行分组 [英] Group array of files by file size to be uploaded
本文介绍了按要上传的文件大小对文件数组进行分组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试创建一个函数,该函数可以将要上传的文件数组分组为10MB或更少的块.例如小规模.
I am trying to create a function that can group an array of files to be uploaded into 10MB or fewer chunks. for example on a small scale.
示例: [1mb,1mb,5mb,4mb,9mb]
预期输出: [[5mb,4mb,1mb],[9mb,1mb]]
我需要该函数迭代一个数字数组并根据最大10mb的大小对它们进行分组.我对实现该目标该怎么做感到困惑.
I need the function to iterate through an array of numbers and group them based on a max of 10mb size. I am a bit confused as to what I should be doing to accomplish this.
谢谢
推荐答案
希望对您有所帮助.
var data = [1, 1, 5, 4, 9];
function getGroups(inputes, maxSize) {
inputes.sort((a, b) => b - a); //sort DESC
var result = [];
while (inputes.length) {
var groups = [];
var sum = inputes[0]; // pick first one (the biggest)
groups.push(inputes[0]);
inputes.splice(0, 1); //remove picked item
var j = 0;
while (j < inputes.length && sum < maxSize) {
if (inputes[j] + sum <= maxSize) {
sum += inputes[j];
groups.push(inputes[j]);
inputes.splice(j, 1);
} else {
j++;
}
}
result.push(groups);
}
return result;
}
console.log(getGroups(data , 10));
这篇关于按要上传的文件大小对文件数组进行分组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文