1

Simple deep clone

Function JavaScript 1 revisions 55 3 months ago 3 months ago
function deepClone(obj) {
	if(Array.isArray(obj)) {
    	return obj.slice().map(copy => {
    	    if(typeof copy === "object") return deepClone(copy);
			else return copy;
		});
	} else if(typeof obj === "object") {
    	return Object.entries(Object.assign({}, obj)).reduce((copy, [key, value]) => {
			if(typeof value === "object") copy[key] = deepClone(value);
			else copy[key] = value;

			return copy;
		}, {});
	} else {
    	return obj;
	}
}

Simply deep clone any object