I have a scenario where i need to copy the array of Objects(Main array) to another Temp array which should not have object reference basically if i make any modification to Main array it should not reflect in the Temp array so that i will preserve the copy independently. Show I have used one of the code snippet from stack overflow this one does partially like if i delete all objects from the Main array the temp array still hold the value but when i do some modifications in main array and click cancel button iam removing all objects from the main array using array.Removeall(); but the modification still exist in Temp array so which means that object having a reference.
my object structure is like iam using knockout framework.
please help me in this regard. Thanks in advance.
Yoshi 53.4k14 gold badges85 silver badges102 bronze badges asked Mar 27, 2012 at 8:27
3 Let me understand: you don't want just have a new array, but you want to create a new instance for all objects are present in the array itself? So if you modify one of the objects in the temp array, that changes is not propagated to the main array? If it's the case, it depends by the values you're keeping in the main array. If these objects are simple objects, and they can be serialized in JSON, then the quickest way is:
If you have more complex objects (like instances created by some your own constructors, html nodes, etc) then you need an approach ad hoc. Edit: If you don't have any methods on your
answered Mar 27, 2012 at 8:40
ZER0ZER0 24k5 gold badges49 silver badges53 bronze badges 5 For some other people with the same question. You could also do it this way.
It's much more functional and idiomatic IMHO
So basically if your objects doesn't have objects as properties. This syntax is everything you need. Unfortunately there is not "out of the box" deep clone feature on the spec but you can always use a library if that's what you need Browser Compatibility Warning: I think it is part of the specification of Ecma now, but some browsers doesn't have full support of spread syntax jet. But using one of the popular transpilers out there you will be fine answered Oct 11, 2018 at 16:28
Iván SánchezIván Sánchez 1,01315 silver badges26 bronze badges 1 Lodash can be used for deep copying objects _.cloneDeep(value)
answered May 29, 2016 at 19:44
tomtomssitomtomssi 9555 gold badges18 silver badges30 bronze badges To copy the values of an array without copying the reference of the array, you can simply do:
This is the recommended solution for AirBnb's JS Style Guide: https://github.com/airbnb/javascript#arrays However, this will not create new referenes for the objects inside the array. To create a new reference for the array and the objects inside, you can do:
answered Nov 13, 2017 at 10:47
2 So you want a deep copy without object reference? Sure, use Example:
PS: I don't think double-JSON parsing is performance wise. answered Mar 27, 2012 at 8:42
Florian MargaineFlorian Margaine 55.8k14 gold badges90 silver badges116 bronze badges 7 You can use Angular's copy: answered Dec 29, 2015 at 8:11
AlikElzin-kilakaAlikElzin-kilaka 32.5k32 gold badges183 silver badges268 bronze badges On nested array you can do:
answered Aug 5, 2021 at 14:30
Use
answered Mar 1, 2018 at 13:12
michal.jakubeczymichal.jakubeczy 6,8861 gold badge52 silver badges57 bronze badges How do you copy an array without references?Use JSON. parse() and JSON. stringify() methods to copy array without reference in JavaScript. If these objects are simple objects, and they can be serialized in JSON.
How do you copy an object in an array?Let's look at several ways of shallowly copying data.. 1 Copying plain objects and Arrays via spreading. We can spread into object literals and into Array literals to make copies: ... . 2 Shallow copying via Object. assign() (optional) ... . 3 Shallow copying via Object. getOwnPropertyDescriptors() and Object.. How do I make a copy of an array in JavaScript?Because arrays in JS are reference values, so when you try to copy it using the = it will only copy the reference to the original array and not the value of the array. To create a real copy of an array, you need to copy over the value of the array under a new value variable.
How do you copy an object in JavaScript?To copy an object in JavaScript, you have three options:. Use the spread ( ... ) syntax.. Use the Object. assign() method.. Use the JSON. stringify() and JSON. parse() methods.. |