Skip to content

Update README updating deepClone function to include case where arrays inside objects were being converted to plain objects during cloning#122

Open
div360 wants to merge 1 commit intoganqqwerty:masterfrom
div360:master
Open

Update README updating deepClone function to include case where arrays inside objects were being converted to plain objects during cloning#122
div360 wants to merge 1 commit intoganqqwerty:masterfrom
div360:master

Conversation

@div360
Copy link
Copy Markdown

@div360 div360 commented Apr 9, 2026

The Problem

When object[key] is an array, typeof object[key] returns 'object' — so it goes into the recursive branch and calls deepClone(array).

Inside deepClone, a fresh {} (plain object) is created, and then for...in loops over the array's numeric indices as keys.

Added a fix to check if it's an array before creating the clone container:
var newObject = Array.isArray(object) ? [] : {};

Without Array.isArray() check, [1, 2, 3] was silently cloned as { "0": 1, "1": 2, "2": 3 } since both arrays and objects return "object" for typeof,fixed by initializing newObject as [] when the input is an array

…ts were being converted to plain objects during cloning
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant