You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

29 lines
789 B

3 years ago
  1. import deepClone from "./deepClone";
  2. // JS对象深度合并
  3. function deepMerge(target = {}, source = {}) {
  4. target = deepClone(target);
  5. if (typeof target !== 'object' || typeof source !== 'object') return false;
  6. for (var prop in source) {
  7. if (!source.hasOwnProperty(prop)) continue;
  8. if (prop in target) {
  9. if (typeof target[prop] !== 'object') {
  10. target[prop] = source[prop];
  11. } else {
  12. if (typeof source[prop] !== 'object') {
  13. target[prop] = source[prop];
  14. } else {
  15. if (target[prop].concat && source[prop].concat) {
  16. target[prop] = target[prop].concat(source[prop]);
  17. } else {
  18. target[prop] = deepMerge(target[prop], source[prop]);
  19. }
  20. }
  21. }
  22. } else {
  23. target[prop] = source[prop];
  24. }
  25. }
  26. return target;
  27. }
  28. export default deepMerge;