feat: Enhance deepMerge function to only update existing keys in target for improved merging logic

This commit is contained in:
Will Miao
2025-08-27 20:42:57 +08:00
parent cd0d832f14
commit 7a8b7598c7

View File

@@ -779,27 +779,37 @@ export class VirtualScroller {
console.log('Virtual scroller enabled'); console.log('Virtual scroller enabled');
} }
// Helper function for deep merging objects // Helper function for deep merging objects - only updates existing keys in target
deepMerge(target, source) { deepMerge(target, source) {
if (!source) return target; if (!source || !target) return target;
const result = { ...target }; const result = { ...target };
Object.keys(source).forEach(key => { // Only iterate over keys that exist in target
if (source[key] !== null && typeof source[key] === 'object' && !Array.isArray(source[key])) { Object.keys(target).forEach(key => {
// If property exists in target and is an object, recursively merge // Check if source has this key
if (target[key] && typeof target[key] === 'object' && !Array.isArray(target[key])) { if (source.hasOwnProperty(key)) {
result[key] = this.deepMerge(target[key], source[key]); const targetValue = target[key];
const sourceValue = source[key];
// If both values are non-null objects and not arrays, merge recursively
if (
targetValue !== null &&
typeof targetValue === 'object' &&
!Array.isArray(targetValue) &&
sourceValue !== null &&
typeof sourceValue === 'object' &&
!Array.isArray(sourceValue)
) {
result[key] = this.deepMerge(targetValue, sourceValue);
} else { } else {
// Otherwise just assign the source value // For primitive types, arrays, or null, use the value from source
result[key] = source[key]; result[key] = sourceValue;
} }
} else {
// For non-objects (including arrays), just assign the value
result[key] = source[key];
} }
// If source does not have this key, keep the original value from target
}); });
return result; return result;
} }