Skip to content
Merged
Changes from 3 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 deepCopy(value) or value
end

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