Replace missing values with default one

Example:

this.themes.basic = {
  background: "black",
  specialThings: {
    border: "1px
  }

}

this.themeObj: {
  background: "black",
  specialThings: {}
}

I need a function that scans this.themeObj and replaces missing values with the value of this.themes.basic. It also should scan child's of the Object. PS: pls don't use eval if it is possible

Thanks

1 answer

  • answered 2019-11-08 13:58 Matteo Ponzo

    You can try with:

    
    let themes = {
      background: "black",
      specialThings: {
        border: "1px"
      }
    }
    
    let themeObj = {
      background: "black",
      specialThings: {}
    }
    
    const object3 = {...themeObj, ...themes };