用默认值替换缺失的值

时间:2019-11-08 13:44:14

标签: javascript object

示例:

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

}

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

我需要一个扫描this.themeObj并替换缺失值的函数 具有this.themes.basic的值。它还应扫描该对象的子对象。 PS:请尽量不要使用eval

谢谢

1 个答案:

答案 0 :(得分:-1)

您可以尝试:


let themes = {
  background: "black",
  specialThings: {
    border: "1px"
  }
}

let themeObj = {
  background: "black",
  specialThings: {}
}

const object3 = {...themeObj, ...themes };