Created
January 2, 2019 12:28
-
-
Save edujr1/8ccf3250729e8dc03e1cb89df3443239 to your computer and use it in GitHub Desktop.
Criar um Objeto clone sem referencias
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Returns a depply cloned object without reference. | |
* Copied from Vue MultiSelect and Vuex. | |
* @type {Object} | |
*/ | |
const deepClone = function (obj) { | |
if (Array.isArray(obj)) { | |
return obj.map(deepClone) | |
} else if (obj && typeof obj === 'object') { | |
var cloned = {} | |
var keys = Object.keys(obj) | |
for (var i = 0, l = keys.length; i < l; i++) { | |
var key = keys[i] | |
cloned[key] = deepClone(obj[key]) | |
} | |
return cloned | |
} else { | |
return obj | |
} | |
} | |
export default deepClone; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment