如何在 JavaScript 中合并 TypedArrays?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14071463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 15:38:25  来源:igfitidea点击:

How can I merge TypedArrays in JavaScript?

javascripttyped-arrays

提问by yomotsu

I'd like to merge multiple arraybuffers to create a Blob. however, as you know, TypedArraydosen't have "push" or useful methods...

我想合并多个数组缓冲区来创建一个 Blob。但是,如您所知, TypedArray没有“推送”或有用的方法......

E.g.:

例如:

var a = new Int8Array( [ 1, 2, 3 ] );
var b = new Int8Array( [ 4, 5, 6 ] );

As a result, I'd like to get [ 1, 2, 3, 4, 5, 6 ].

结果,我想得到[ 1, 2, 3, 4, 5, 6 ].

回答by Prinzhorn

Use the setmethod. But note, that you now need twice the memory!

使用set方法。但请注意,您现在需要两倍的内存!

var a = new Int8Array( [ 1, 2, 3 ] );
var b = new Int8Array( [ 4, 5, 6 ] );

var c = new Int8Array(a.length + b.length);
c.set(a);
c.set(b, a.length);

console.log(a);
console.log(b);
console.log(c);

回答by D?nu

I always use this function:

我总是使用这个功能:

function mergeTypedArrays(a, b) {
    // Checks for truthy values on both arrays
    if(!a && !b) throw 'Please specify valid arguments for parameters a and b.';  

    // Checks for truthy values or empty arrays on each argument
    // to avoid the unnecessary construction of a new array and
    // the type comparison
    if(!b || b.length === 0) return a;
    if(!a || a.length === 0) return b;

    // Make sure that both typed arrays are of the same type
    if(Object.prototype.toString.call(a) !== Object.prototype.toString.call(b))
        throw 'The types of the two arguments passed for parameters a and b do not match.';

    var c = new a.constructor(a.length + b.length);
    c.set(a);
    c.set(b, a.length);

    return c;
}

The original function without checking for null or types

不检查 null 或类型的原始函数

function mergeTypedArraysUnsafe(a, b) {
    var c = new a.constructor(a.length + b.length);
    c.set(a);
    c.set(b, a.length);

    return c;
}

回答by JerryCauser

for client-side ~ok solution:

对于客户端 ~ok 解决方案:

const a = new Int8Array( [ 1, 2, 3 ] )
const b = new Int8Array( [ 4, 5, 6 ] )
const c = Int8Array.from([...a, ...b])

回答by hoogw

if I have multiple typed arrays

如果我有多个类型化数组

            arrays = [ typed_array1, typed_array2,..... typed_array100]

I want concat all 1 to 100 sub array into single 'result' this function works for me.

我想将所有 1 到 100 个子数组连接到单个“结果”中,这个函数对我有用。

  single_array = concat(arrays)


function concat(arrays) {
  // sum of individual array lengths
  let totalLength = arrays.reduce((acc, value) => acc + value.length, 0);

  if (!arrays.length) return null;

   let result = new Uint8Array(totalLength);

      // 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;
   }

回答by randomsock

As a one-liner, which will take an arbitrary number of arrays (myArrayshere) and of mixed types so long as the result type takes them all (Int8Arrayhere):

作为单行,它将采用任意数量的数组(myArrays此处)和混合类型,只要结果类型全部采用(Int8Array此处):

let combined = Int8Array.from(Array.prototype.concat(...myArrays.map(a => Array.from(a))));

回答by mattdlockyer

I like @prinzhorn's answer but I wanted something a bit more flexible and compact:

我喜欢@prinzhorn 的回答,但我想要一些更灵活和紧凑的东西:

var a = new Uint8Array( [ 1, 2, 3 ] );
var b = new Float32Array( [ 4.5, 5.5, 6.5 ] );

const merge = (tArrs, type = Uint8Array) => {
  const ret = new (type)(tArrs.reduce((acc, tArr) => acc + tArr.byteLength, 0))
  let off = 0
  tArrs.forEach((tArr, i) => {
    ret.set(tArr, off)
    off += tArr.byteLength
  })
  return ret
}

merge([a, b], Float32Array)