clone object in js
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 = {key: 'value'}
var y = JSON.parse(JSON.stringify(x))
//this method actually creates a reference-free version of the object, unlike the other methods
//If you do not use Dates, functions, undefined, regExp or Infinity within your object//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:
- what is an iterator in javascript
- javascript array get nth element
- how to update react app
- how to add okta api to vuejs
- appendchild element once if element presense in js
- mongoose max record
- class component react
- javascript x number of parameters
- onswitch from tab do something jquery
- javascript var in quotes
- express pourquoi mettre bodyparser avant router
- class inheritance method javascript
- short string javascript
- mongoose find and update prop
- how to make modules structure like lodash
- how to make a rectangle in javascript
- how to use react fragment
- how to remove property of object in javascript without delete
- How to make PWAs installable js
- create array javascript fill
- button click javascript by class
- fetch
- javascript do arrays intersect
- send json body http get flutter