I have few JS objects. They can have any structure:
{
  name: "first",
  _ref_id: 1234,
  spec: {
    _ref_id: 2345,
    data: "lots of data"
  }
}
{
  name: 'second',
  _ref_id: 5678,
  container: {
    _ref_id: 6789,
    children: [
      {_ref_id: 3214, name: 'Bob'}
      {_ref_id: 1111, name: 'Mark'}
      {_ref_id: 2222, name: 'Frank'}
    ]
  }
}
Problem:
I need to make a copies of this object but with a different _ref_ids.
Creation of the 'first' object my look like this:
first = {
  name: "first",
  _ref_id: uuid.v4(),
  spec: {
    _ref_id: uuid.v4(),
    data: "lots of data"
  }
}
So I know the structure of the object when I am creating it but further down the chain in a place where I am trying to make a copy of this object I don't have access and I don't know what is the structure of this object all I have is the object it self. So after coping 'first' I would like to have:
{
  name: "first",
  _ref_id: 8888,
  spec: {
    _ref_id: 9999,
    data: "lots of data"
  }
} 
I tried instead of defining the _ref_id a simple value a sort of memoized function during the object creation:
refId(memoized = true){
  var memo = {}
  return () => {
    if(!memoized) memo = {}
    if(memo['_ref_id'])
      return memo._ref_id
    else {
      memo._ref_id = uuid.v4()
      return memo._ref_id
    }
  }
}
So I can create it:
first = {
  name: "first",
  _ref_id: refId(),
  spec: {
    _ref_id: refId(),
    data: "lots of data"
  }
}
And change the first._ref_id to first._ref_id() whenever I am trying to access the value of it.
But I have no idea how to reset the memoized variable from inside the coping function or if this is even possible?
Have anyone had similar problem? Maybe there is a different way to solve it?
PS:
I am using lodash and immutable.js in this project but I haven't found any helper functions for this particular task.
 
     
    