Why Does this code work, but this doesn't?

thanks @Jarda , that’d be great for everyone!

until then you can use:
Array.from(app.activeDocument.layers)
which return a normal array of layers

or the following function I created to get ALL LAYERS regardless of “depth”
it accepts one argument : the root of layers
it could be doc’s layers or a group of layers

function flatLayers(lys) {
   return lys.flatMap((v) => v.layers ? [v].concat(flatLayers(v.layers)) : v)
}

and use it like so:
flatLayers(app.activeDocument.layers)

3 Likes