Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New DeepClone code snippet #847

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 13 additions & 6 deletions content/en-us/luau/tables.md
Original file line number Diff line number Diff line change
Expand Up @@ -291,14 +291,21 @@ local clone = table.clone(original)
To copy a more complex table with nested tables inside it, you'll need to use a recursive function similar to the following:

```lua
local function deepCopy(original)
-- The function used for deep copying a table.
local function deepCopy(original)
-- Define the new table for the copy
local copy = {}
for k, v in pairs(original) do
if type(v) == "table" then
v = deepCopy(v)
end
copy[k] = v

-- Loop through the original table to clone
for key, value in original do
-- If the type of the value is a table,
-- then deep copy it to the key (index).
-- Else (or) the type isn't a table,
-- assign the default value to the index instead.
copy[key] = type(value) == "table" and MY_deepCopy(value) or value
end

-- Return the finalized copy of the deep cloned table
return copy
end
```
Expand Down
Loading