How can I merge TypedArrays in JavaScript?

后端 未结 7 491
说谎
说谎 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:42

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

    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);
    

提交回复
热议问题