How can I merge TypedArrays in JavaScript?

后端 未结 7 454
说谎
说谎 2020-12-09 01:01

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

E.g.:

var a          


        
相关标签:
7条回答
  • 2020-12-09 01:49

    For people who love one-liners:

      const binaryData = [
        new Uint8Array([1, 2, 3]),
        new Int16Array([4, 5, 6]),
        new Int32Array([7, 8, 9])
      ];
    
      const mergedUint8Array = new Uint8Array(binaryData.map(typedArray => [...new Uint8Array(typedArray.buffer)]).flat());
    
    0 讨论(0)
提交回复
热议问题