deepMerge.js 843 B

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