Skip to content

Commit a449111

Browse files
authored
Merge branch 'main' into patch-1
2 parents fc89436 + 3ff4d1d commit a449111

File tree

1 file changed

+11
-6
lines changed

1 file changed

+11
-6
lines changed

content/en-us/luau/tables.md

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,19 @@ local clone = table.clone(original)
291291
To copy a more complex table with nested tables inside it, you'll need to use a recursive function similar to the following:
292292

293293
```lua
294-
local function deepCopy(original)
294+
-- The function used for deep copying a table
295+
local function deepCopy(original)
296+
-- Define the new table for the copy
295297
local copy = {}
296-
for k, v in pairs(original) do
297-
if type(v) == "table" then
298-
v = deepCopy(v)
299-
end
300-
copy[k] = v
298+
299+
-- Loop through the original table to clone
300+
for key, value in original do
301+
-- If the type of the value is a table, deep copy it to the key (index)
302+
-- Else (or) the type isn't a table, assign the default value to the index instead
303+
copy[key] = type(value) == "table" and deepCopy(value) or value
301304
end
305+
306+
-- Return the finalized copy of the deep cloned table
302307
return copy
303308
end
304309
```

0 commit comments

Comments
 (0)