What are good ways to create a copy of an object in javascript?
-
I need to create a copy of a JSON object. Methods like Object.create(), new Object() didnt help me. the new objects created using these methods always takes the reference of the original object
-
Answer:
I've developed quite a few webapps and I must tell you that I only wanted to copy entire json objects when I was a beginner. There are very few cases when you actually have to do this. If you are using jquery, $.extend with a recursive parameter set to true will do the trick. Basically you have to iterate over each key in the json object, and if the key is a primitive type just copy that value with normal attribution (=). If the key is an object itself assign to the key of the resulting object the result of calling the same copy function again on the sub object that you are iterating over. This is a pretty boring code. Jquery and extjs offer the extend method for this.
Vlad Nicula at Quora Visit the source
Other answers
ES6 feature Object.prototype.assign() It is used to copy all of the "own" properties from source object(s) on to a target object. So it won't copy any properties that are inherited from an object's prototype. Usage: let sourceObj = { a: 12, b(x) { return x *x }, c: "Some string" } let targetObj = {}; let clone = Object.assign({}, sourceObj); console.log(targetObj === clone); // true https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/Object/assign
Ryan Tomczik
boring codeï¼ function clone(obj) { if (null == obj || "object" != typeof obj) return obj; var copy = obj.constructor(); for (var attr in obj) { if (obj.hasOwnProperty(attr)) { if (Object.getOwnPropertyDescriptor(obj, attr).value instanceof Object) { copy[attr] = clone(obj[attr]); } else { Object.defineProperty(copy, attr, Object.getOwnPropertyDescriptor(obj,attr)); } } } return copy; }
Cai Yiheng
Related Q & A:
- What are good ways to save up for an iPod Touch?Best solution by wikihow.com
- What are good ways to promote my website?Best solution by Yahoo! Answers
- What are easy ways of writing a good essay?Best solution by Quora
- What are some good ways to advertise a business?Best solution by ChaCha
- What are good ways to get the "Last Second Bid" on eBay successfully?Best solution by ebay.com
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.