clone an object javascript
var student = {name: "Rahul", age: "16", hobby: "football"};
//using ES6
var studentCopy1 = Object.assign({}, student);
//using spread syntax
var studentCopy2 = {...student};
//Fast cloning with data loss
var studentCopy3 = JSON.parse(JSON.stringify(student));let clone = Object.assign({}, objToClone);var x = {myProp: "value"};
var xClone = Object.assign({}, x);
//Obs: nested objects are still copied as reference.//returns a copy of the object
function clone(obj) {
if (null == obj || "object" != typeof obj) return obj;
var copy = obj.constructor();
for (var attr in obj) {
if (obj.hasOwnProperty(attr)) copy[attr] = obj[attr];
}
return copy;
}
Also in JavaScript:
- get caret position javascript
- javascript remove object property
- array sort
- js array to json
- Programmatically navigate using react router
- open pdf in browser javascript
- jquery if attribute
- jquery run after page load
- node js module require multiple functions from one node
- reverse every word in a string javascript
- get width of a dom element js
- get string value of boolean javascript
- moment format datetime postgresql
- what is ajax
- index and id together angularjs
- python json dump format
- javascript function convert bytes into mb
- angular date formats
- js onscroll event
- how to stop the node server from running
- how to reload webview in react native
- user input through js
- is react context better than redux
- how do i check if JQuery checkbox is checked