1

I have a complete config object and another object that can be partially filled.

When merging them, I need the unchanged settings to remain.

let defaultConfig = {
  locale: 'en',
  messages: {
    mixed: {
      required: 'is required',
      confirmed: 'is confirmed'
    }
  },
  // outher configs...
}

let newConfig = {
  locale: 'pt',
  messages: {
    mixed: {
      required: 'é obrigatório'
    }
  }
}


defaultConfig = { ...defaultConfig, ...newConfig }

console.log(defaultConfig)

Result of the code above:

{
  "locale": "pt",
  "messages": {
    "mixed": {
      "required": "é obrigatório"
    }
  }
} 

The expected result:

{
  "locale:" "pt",
  "messages": {
    "mixed": {
      "required": "é obrigatório",
      "confirmed": "is confirmed"
    }
  }
}

Remember that there are other settings/properties, so anyway it needs to have merge logic.

halfer
  • 19,824
  • 17
  • 99
  • 186
Yung Silva
  • 1,324
  • 4
  • 20
  • 40
  • 1
    Does this answer your question? [How to deep merge instead of shallow merge?](https://stackoverflow.com/questions/27936772/how-to-deep-merge-instead-of-shallow-merge) – Nick Bailey Jan 27 '22 at 03:20

0 Answers0