In addition to recursion, iteration can also be used to achieve deep copy. Specifically, a stack can be used to store the objects that need to be processed, and then in a loop, objects are continuously popped from the stack and copied until the stack is empty.
Here is an example code that uses iteration to achieve deep copy:
function deepClone(obj) {
let stack = [obj];
let copy = {};
while (stack.length) {
let cur = stack.pop();
for (let key in cur) {
if (cur.hasOwnProperty(key)) {
let value = cur[key];
if (typeof value === 'object') {
// Handling arrays
if (Array.isArray(value)) {
copy[key] = [];
for (let i = 0; i < value.length; i++) {
copy[key][i] = value[i];
if (typeof value[i] === 'object') {
stack.push(value[i]);
}
}
}
// Handling objects
else {
copy[key] = {};
for (let k in value) {
copy[key][k] = value[k];
if (typeof value[k] === 'object') {
stack.push(value[k]);
}
}
}
} else {
copy[key] = value;
}
}
}
}
return copy;
}