0

How do you deep clone an array of objects "the old way." In other words, before the introduction of methods of modern, native methods like JSON.parse, assign, extend, or the spread operator.

The solution is for some Photoshop scripts. Adobe is still using JS v1.5 (or ECMAScript 3) - meaning any JS functionality since 2000 isn't native. Ideal solutions would deep clone but avoid Photoshop specific objects, such as "layers" (more on that at the end).

Alternatively, a lightweight library mocking modern functionality would work. I've already tried this JSON2 implementation but ran into the following error:

Error 8450: The operation is not valid for channels of type component.

I came across this JSON resource, but the guide seemed daunting, so I haven't gotten far yet.

The array of objects is currently structured like so:

object = [
    {
        prop: string,
        array: [{layerObject},{layerObject}...]
    },
    {...},
]

Ideally all of the above except the 'layerObjects' would be cloned. The layerObjects should not be cloned because they are references to onscreen Photoshop elements that need to be tracked and manipulated.

Zollie
  • 381
  • 3
  • 16
  • Many of the answers to the linked dupetarget use only ES3-level features. (Separately: `JSON.parse` should not be used for object cloning, as JSON lacks many features JavaScript objects have.) – T.J. Crowder Jan 04 '19 at 09:41
  • Thanks for the quick response @T.J.Crowder. Would those ES3-level features be assign or extend? Because Photoshop throws unknown errors for both methods. – Zollie Jan 04 '19 at 09:48
  • 1
    No, ES3 didn't have `Object.assign`, it was added in ES2015. (`extend` isn't part of JavaScript at all, though you can write it in ES3 [and they did in jQuery].) ES3 had `for-in`, `hasOwnProperty`, `typeof`, and object initializers (object literals) though, which is all many of those answers use. – T.J. Crowder Jan 04 '19 at 09:50

0 Answers0