Search code examples
javascriptarraysjsonstringarraybuffer

Cannot get the value of an ArrayBuffer Object in Javascript


I have an ArrayBuffer object that I need to be able to convert to String to JSON, but I can't get the value of the [Int8Array] out of the object, even though it is clearly there.

enter image description here

I've tried all variations on this, but they all return undefined

console.log(result);//Returns the array buffer
//Following methods all return undefined?
console.log(result["[[Int8Array]]"]);
console.log(result[[[Int8Array]]]);
console.log(result[[["Int8Array"]]]);
console.log(result[Int8Array]);
console.log(result["Int8Array"]);

How can I get all the Int8Array or UInt8Array values that are clearly available in the object?


Solution

  • You need to intiantiate a new Uint8Array to get their values, you can't access them directly using your ArrayBuffer instance.

    var buf = new ArrayBuffer(8);
    var int8view = new Uint8Array(buf);
    console.log(int8view)

    JSFiddle : https://jsfiddle.net/v8m7pjqb/