0

Similar to this question Remove a property in an object immutably

I have an object

  parent {
     child {
       grandchild {},
     },
  },

I'm trying to flatten in a reducer

   parent: {}
   child:  {}
   grandchild: {}

With a merge function that breaks the objects up based on some requirements.

merge(state, object){

  ....
  if(parent.child) mergeAndDelete(state, parent.child, parent, 'child')

}


function mergeAndDelete(state, type, id, ent, attrib) {
  const clone = cloneDeep(ent[attrib]);
  merge(state, type, id, clone);
  delete ent[attrib];
}

If I delete the entity, even after the merge then the child and grandchild never make it to the reducer. They console log the whole way through. If I don't delete then they get merged as I want. But parent still has child etc.

What is the best way of going about this?

beek
  • 3,158
  • 6
  • 25
  • 74
  • What happens if there are duplicate (conflicting) property names at multiple nested levels? Also, please edit your question to provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) (ideally with a link to the same code in the [TypeScript Playground](https://www.typescriptlang.org/play?noUncheckedIndexedAccess=true&target=99&useUnknownInCatchVariables=true&exactOptionalPropertyTypes=true#code/Q) so that there's minimal friction for potential answerers to help you.) – jsejcksn Apr 13 '22 at 23:42

0 Answers0