连接类型化数组
给定一个 Uint8Array
数组,编写一个函数 concat(arrays)
,该函数将它们连接成一个数组并返回。
function concat(arrays) {
// sum of individual array lengths
let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);
let result = new Uint8Array(totalLength);
if (!arrays.length) return result;
// for each array - copy it over result
// next array is copied right after the previous one
let length = 0;
for(let array of arrays) {
result.set(array, length);
length += array.length;
}
return result;
}