object.ts 552 B

1234567891011121314151617
  1. /**
  2. * 将值复制到目标对象,且以目标对象属性为准,例:target: {a:1} source:{a:2,b:3} 结果为:{a:2}
  3. * @param target 目标对象
  4. * @param source 源对象
  5. */
  6. export const copyValueToTarget = (target, source) => {
  7. const newObj = Object.assign({}, target, source)
  8. // 删除多余属性
  9. Object.keys(newObj).forEach((key) => {
  10. // 如果不是target中的属性则删除
  11. if (Object.keys(target).indexOf(key) === -1) {
  12. delete newObj[key]
  13. }
  14. })
  15. // 更新目标对象值
  16. Object.assign(target, newObj)
  17. }