0

I have a JavaScript array of objects, which is initialised with some values. I want to copy those values to a new array of objects, but without referencing to the original one, so I can then manipulate the new object.

I tried objectOptions.concat() like in this sample code but it then deletes also the referenced original object:

var objectOptions = [{option1: 'value1', someOption: 'value2'}];

function objectClean(){
    var newObjectOptions = objectOptions.concat();

    for(var i in newObjectOptions ) {
        delete newObjectOptions[i]['someOption'];
    }

    return newObjectOptions ;
};
Vassilis Barzokas
  • 3,105
  • 2
  • 26
  • 41
  • 1
    http://stackoverflow.com/questions/122102/what-is-the-most-efficient-way-to-clone-a-javascript-object – jeremy Jan 26 '13 at 17:51
  • Thank you! I didn't think of jQuery and i was trying native Javascript methods. It can be closed. – Vassilis Barzokas Jan 26 '13 at 17:55
  • 1
    What?? No, don't use jQuery! Where does it say to use jQuery? – jeremy Jan 26 '13 at 18:06
  • In the accepted answer of the question you posted. I tried it and it works fine for what i want to do. – Vassilis Barzokas Jan 26 '13 at 18:09
  • That answer was written by the creator of jQuery, and yes, it works *if you're using jQuery*. But having to use jQuery just for that is nonsense (I guess that's what @Nile means). It's terrible that that question and answer are so popular, because people will be misguided, thinking they need jQuery to clone an object. And unfortunately there are many questions closed as a duplicate of that one. – bfavaretto Jan 26 '13 at 18:25
  • Of course i am not going to use jQuery just for this, otherwise i would insist on asking only native methods. I am already using it in my code, so i found it very useful to use this one line instead of writing some new method for it. Thank you however. – Vassilis Barzokas Jan 26 '13 at 18:30
  • @SportBilly Sorry if I misunderstood you. I thought you weren't using jQuery, since your code has no traces of it, and your question is not tagged jQuery. Furthermore, it's very common for people here to think they need jQuery just to be able to accomplish very simple tasks, so I try to clarify that's not the case everytime I can. – bfavaretto Jan 26 '13 at 18:40

2 Answers2

3

If your object is simple enough and JSON-compatible (i.e., only contains objects, arrays and primitives, no DOM references, no Regex objects, no circular references etc.), the simplest way to clone is:

var clone = JSON.parse(JSON.stringify(original));

Otherwise, you'll need a deep copy function, like the one found on this answer*:

function clone(obj){
    if(obj == null || typeof(obj) != 'object')
        return obj;

    var temp = obj.constructor(); // changed

    for(var key in obj)
        temp[key] = clone(obj[key]);
    return temp;
}

* Be careful! The accepted answer to that question is a jQuery only solution, and terribly overrated!

Community
  • 1
  • 1
bfavaretto
  • 71,580
  • 16
  • 111
  • 150
1
var copyme = {/*object with properties which are to be copied in another object*/};
var copy = {};
for (var attr in copyme) {
  if (copyme.hasOwnProperty(attr)) copy[attr] = clone(copyme[attr]);
}
Patt Mehta
  • 4,110
  • 1
  • 23
  • 47
  • ask a question, do not be chatty in comments – Patt Mehta Jan 26 '13 at 17:58
  • Did the asker explain what is concat ? – Patt Mehta Jan 26 '13 at 18:00
  • Your answer is theoretical until you can actually justify it by explaining how it works. Have you studied the flagging system? I have the evidential means to both flag and downvote your answer. – David G Jan 26 '13 at 18:04
  • But when I explain with correct answers can I be assured that I'll get upvotes : http://stackoverflow.com/questions/14505840/keep-value-in-form-after-submitting-php/14505992#14505992 http://stackoverflow.com/questions/14435426/align-all-content-with-the-bottom-of-the-page/14435875#14435875 – Patt Mehta Jan 26 '13 at 18:06
  • Not necessarily, and that isn't my point. It's that it's always convenient for both the asker and the common reader to actually *understand* the code they may come upon. What if the OP is a beginner? Then he is using code, the workings of which, he can't understand. – David G Jan 26 '13 at 18:19
  • 1
    That means you are agreeing it is difficult for newcomers to grow here, simply because someone cannot put just that little effort to click with that hefty mouse on the up-vote button. – Patt Mehta Jan 26 '13 at 18:21