Access properties of a big object to map them but only if they are under the same Parent and have the same name property and change the name prop

I would like to map this big object to access and edits its property just if they are under the same parent and have the same name(like the children count setting to the childrenId.length or to edit the name based on other properties)

So basically given this DemoMap object I am interested in the fileMap sub-object and operate within it. Note that every object name is the id name

  var DemoMap = {
  firstPropID: "rootID",
  fileMap: {
    rootID: {
      id: "rootID",
      name: "root",
      isDir: true, 
      childrenIds: ["ParentA_ID","Child3_id","Child3_2_id"], 
      childrenCount: 3, //childrenId array.length
    },
    ParentA_ID: {
      id: "ParentA_ID",
      name: "ParentA",
      isDir: true,
      modDate: "2020-10-24T17:48:39.866Z",
      childrenIds: ["Child1_id", "Child2_id"],
      childrenCount: ["Child1_id", "Child2_id"].length,
      parentId: "rootID",
    },
    Child1_id: {
      id: "Child1_id",
      name: "Child1",
      isDir: false,
      modDate: "2020-10-24T17:48:39.866Z",
      childrenIds: [],
      childrenCount: 0,
      parentId: "ParentA_ID",
    },
    Child2_id: {
      id: "Child2_id",
      name: "Child2",
      isDir: false,
      modDate: "2020-10-24T17:48:39.866Z",
      childrenIds: [],
      childrenCount: 0,
      parentId: "ParentA_ID",
    },
    Child3_id: {
      id: "Child3_id",
      name: "Child3",
      isDir: false,
      modDate: "2020-10-24T17:48:39.866Z",
      childrenIds: [],
      childrenCount: 0,
      parentId: "rootID",
    },
      Child3_2_id: {
      id: "Child3_2_id",
      name: "Child3",
      isDir: false,
      modDate: "2020-10-24T17:48:39.866Z",
      childrenIds: [],
      childrenCount: 0,
      parentId: "rootID",
    }
 
  },

Given that I expect to be able to act on the Child3_id and Child3_2_id that have different ids and same name and under the same root parent and change the name with a progressive number like that

RESULT EXPECTED

...
Child3_id: {
      id: "Child3_id",
      name: "Child3 (1 of 2)",
      isDir: false,
      modDate: "2020-10-24T17:48:39.866Z",
      childrenIds: [],
      childrenCount: 0,
      parentId: "rootID",
    },
      Child3_2_id: {
      id: "Child3_2_id",
      name: "Child3 (2 of 2)",
      isDir: false,
      modDate: "2020-10-24T17:48:39.866Z",
      childrenIds: [],
      childrenCount: 0,
      parentId: "rootID",
    }
 
  },

Answer

We can recurse through the Object, then build a map of object names.

If we find any duplicate names in this map, we iterate through these and rename the relevant objects:

const DemoMap = { firstPropID: "rootID", fileMap: { rootID: { id: "rootID", name: "root", isDir: true, childrenIds: ["ParentA_ID","Child3_id","Child3_2_id"], childrenCount: 3, }, ParentA_ID: { id: "ParentA_ID", name: "ParentA", isDir: true, modDate: "2020-10-24T17:48:39.866Z", childrenIds: ["Child1_id", "Child2_id"], childrenCount: ["Child1_id", "Child2_id"].length, parentId: "rootID", }, Child1_id: { id: "Child1_id", name: "Child1", isDir: false, modDate: "2020-10-24T17:48:39.866Z", childrenIds: [], childrenCount: 0, parentId: "ParentA_ID", }, Child2_id: { id: "Child2_id", name: "Child2", isDir: false, modDate: "2020-10-24T17:48:39.866Z", childrenIds: [], childrenCount: 0, parentId: "ParentA_ID", }, Child3_id: { id: "Child3_id", name: "Child3", isDir: false, modDate: "2020-10-24T17:48:39.866Z", childrenIds: [], childrenCount: 0, parentId: "rootID", }, Child3_2_id: { id: "Child3_2_id", name: "Child3", isDir: false, modDate: "2020-10-24T17:48:39.866Z", childrenIds: [], childrenCount: 0, parentId: "rootID", } } };

function updateKeys(obj, parent = '') {
    let names = {};
    for (let key of Object.keys(obj)) {
        if (typeof obj[key] === "object") {
            updateKeys(obj[key], key);
            if (obj[key].name && obj[key].parentId) {
                let mapKey = obj[key].name + obj[key].parentId;
                names[mapKey] = (names[mapKey] || []).concat(obj[key].id)
            }
        }
    }
    const duplicatekeys = Object.keys(names).filter(a => names[a].length > 1);
    for(let dupKey of duplicatekeys) {
        names[dupKey].forEach((key, idx) => obj[key].name = `${obj[key].name} (${idx+1} of ${names[dupKey].length})` )
    }
}

updateKeys(DemoMap)
console.log('Result:', JSON.stringify(DemoMap, null, 2))