0

How can I deep clone an object in JavaScript which is not a standard type of object such as the ImageData? It contains another object of type UInt64Array which I also want not referencing a separate object, but fully copying it.

All other questions regarding deep copying only deal with simple data types such as Object, Array, etc. Those methods, including jQuery's extend function, don't work with other data types.

When trying to use jQuery's extend (which seems to be able to copy the most different types) I just get a standard object out, losing the type that I need. putImageData will only accept an ImageData object.

Chris Cook
  • 474
  • 2
  • 18
  • you can clone deep clone any object in js using JSON.parse(JSON.stringify(obj)) https://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-deep-clone-an-object-in-javascript – user93 Jul 02 '17 at 14:37
  • what objects are you trying to clone please insert the code to explain further – user93 Jul 02 '17 at 14:38
  • The JSON.parse, stringify method isn't reliable since it leaves omits functions. – Khauri Jul 02 '17 at 14:51
  • Exactly. This is written everywhere. It works for very simple objects only. @user93 the datatype is in the question - give it a read. – Chris Cook Jul 02 '17 at 14:53

1 Answers1

1

imageData is a Uint8ClampedArray You can clone it and other arrays by using TypedArray#from. In most cases Array.from(TypedArray) will clone it.

let imageData = new Uint8ClampedArray([1,2,3])
let arrayClone = Array.from(imageData)
let ui8caClone = Uint8ClampedArray.from(imageData)

arrayClone[0] = 5;
ui8caClone[0] = 9;
Khauri
  • 3,753
  • 1
  • 11
  • 19
  • Thanks, I think this is on the right track, but ImageData isn't a Uint8ClampedArray, ImageData.data is. I'll have a look and see if I can get this working and accept it if it does. – Chris Cook Jul 02 '17 at 14:50