FED16. 简易深拷贝
描述
请补全JavaScript代码,要求实现对象参数的深拷贝并返回拷贝之后的新对象。HTML/CSS/JavaScript 解法, 执行用时: 1706ms, 内存消耗: 77800KB, 提交时间: 2022-02-09
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n \t\n <script type=\"text/javascript\">\n const _sampleDeepClone = target => {\n // 补全代码\n const type = Object.prototype.toString.call(target).slice(8, -1)\n if(type === 'Object' || type === 'Array') {\n const res = Array.isArray(target) ? [] : {}\n for(let key in target) {\n if(target.hasOwnProperty(key)) {\n res[key] = _sampleDeepClone(target[key])\n }\n }\n return res\n } else {\n return target\n }\n }\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1738ms, 内存消耗: 77880KB, 提交时间: 2022-01-25
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n \t\n <script type=\"text/javascript\">\n const _sampleDeepClone = target => {\n // 补全代码\n if(typeof target !='object')return target;\n const type = Object.prototype.toString.call(target);\n console.log(type)\n const obj = type == '[object Object]'?{}:[];\n for(let key in target){\n if(typeof target[key]!='object'){\n obj[key] = target[key]\n }else{\n obj[key] = _sampleDeepClone(target[key])\n }\n }\n return obj\n }\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1740ms, 内存消耗: 77772KB, 提交时间: 2021-12-14
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n \t\n <script type=\"text/javascript\">\n const _sampleDeepClone = target => {\n // 补全代码\n if(typeof target !== 'object' || target == null) {\n return target\n }\n const cloneTarget = Array.isArray(target) ? [] : {}\n for(let key in target) {\n if(target.hasOwnProperty(key)) {\n if(target[key] && typeof target[key] == 'object') {\n cloneTarget[key] = _sampleDeepClone(target[key])\n } else {\n cloneTarget[key] = target[key]\n }\n }\n }\n return cloneTarget\n }\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1741ms, 内存消耗: 77768KB, 提交时间: 2021-12-14
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n \t\n <script type=\"text/javascript\">\n const _sampleDeepClone = target => {\n // 补全代码\n if (typeof target !== 'object') {\n return target;\n }\n \n let res = null;\n if (Array.isArray(target)) {\n res = [];\n target.forEach(item => {\n res.push(_sampleDeepClone(item));\n })\n } else {\n res = {};\n for (const key in target) {\n res[key] = _sampleDeepClone(target[key]);\n }\n \n }\n \n return res;\n }\n </script>\n </body>\n</html>","libs":[]}
HTML/CSS/JavaScript 解法, 执行用时: 1742ms, 内存消耗: 77920KB, 提交时间: 2022-01-23
{"css":"","js":"","html":"<!DOCTYPE html>\n<html>\n <head>\n <meta charset=utf-8>\n </head>\n <body>\n \t\n <script type=\"text/javascript\">\nconst _sampleDeepClone = target => {\n // 补全代码\n let newObj = target instanceof Array ? [] : {}\n for(let i in target){\n if(target.hasOwnProperty(i)){\n newObj[i] = typeof target[i] == 'object' ? _sampleDeepClone(target[i]) : target[i]\n }\n }\n return newObj\n }\n\n\n </script>\n </body>\n</html>","libs":[]}