Option for DeepCopier to also deep-copy objects

The DeepCopier currently does not deep-copy basic objects, eg.:

let obj = {
  nested: {
    nested2: "test";
  }
}

PG: Babylon.js Playground

In addition to the shallowCopyValues option there should be a deepCopyValues option.

1 Like

I wrote a merge function for a sort of deep copy, but more like a nested copy. Full deep copy gets complex quickly as questions come up about arrays. Do you replace, interlace, sort and/or append?

Here’s a merge and an unmerge (e.g. subtract) for simple nested javascript objects:

1 Like

For deep-copying objects I’m using this recursive function, which is enough for my use-case:

let obj = deepCopyObject(obj2);

 function deepCopyObject(src) {
    let target = Array.isArray(src) ? [] : {};
    for (let key in src) {
      let v = src[key];
      if (v) {
        if (typeof v === "object") {
          target[key] = deepCopyObject(v);
        } else {
          target[key] = v;
        }
        if (v instanceof BABYLON.Vector3) {
          target[key] = new BABYLON.Vector3(v.x, v.y, v.z);
        }
        if (v instanceof BABYLON.Vector2) {
          target[key] = new BABYLON.Vector2(v.x, v.y);
        }
      } else {
        target[key] = v;
      }
    }

    return target;
  }

But I’d like to have a built in object deep copier in the BJS DeepCopier for ease, so I don’t have to paste this function everywhere.

1 Like

Sadly not compatible with older browser versions (pre-2022ish) :confused:

93.28%


95.85%


3 Likes