Skip to main content
code edit
Source Link
Jack_of_All_Trades
  • 11.6k
  • 20
  • 64
  • 96

Okay, This might be a strange solution but it is plain javascript. If you want to clone something, I would go with the word, "deep copy", you can use JSON like this:

var obj = {
    name: "abc",
    age: 20
}


new_object=JSON.parse(JSON.stringify(obj));

Now, you have clone of the object obj.

Another solution is like this:

var new_obj={};
for( new_prop in aobj){
    if (obj.hasOwnProperty(new_prop)){
          new_obj[new_prop]=obj[new_prop]
     }
 }

Okay, This might be a strange solution but it is plain javascript. If you want to clone something, I would go with the word, "deep copy", you can use JSON like this:

var obj = {
    name: "abc",
    age: 20
}


new_object=JSON.parse(JSON.stringify(obj));

Now, you have clone of the object obj.

Another solution is like this:

var new_obj={};
for( new_prop in a){
    if (obj.hasOwnProperty(new_prop)){
          new_obj[new_prop]=obj[new_prop]
     }

Okay, This might be a strange solution but it is plain javascript. If you want to clone something, I would go with the word, "deep copy", you can use JSON like this:

var obj = {
    name: "abc",
    age: 20
}


new_object=JSON.parse(JSON.stringify(obj));

Now, you have clone of the object obj.

Another solution is like this:

var new_obj={};
for( new_prop in obj){
    if (obj.hasOwnProperty(new_prop)){
          new_obj[new_prop]=obj[new_prop]
     }
 }
code added
Source Link
Jack_of_All_Trades
  • 11.6k
  • 20
  • 64
  • 96

Okay, This might be a strange solution but it is plain javascript. If you want to clone something, I would go with the word, "deep copy", you can use JSON like this:

var obj = {
    name: "abc",
    age: 20
}


new_object=JSON.parse(JSON.stringify(obj));

Now, you have clone of the object obj.

Another solution is like this:

var new_obj={};
for( new_prop in a){
    if (obj.hasOwnProperty(new_prop)){
          new_obj[new_prop]=obj[new_prop]
     }

Okay, This might be a strange solution but it is plain javascript. If you want to clone something, I would go with the word, "deep copy", you can use JSON like this:

var obj = {
    name: "abc",
    age: 20
}


new_object=JSON.parse(JSON.stringify(obj));

Now, you have clone of the object obj.

Okay, This might be a strange solution but it is plain javascript. If you want to clone something, I would go with the word, "deep copy", you can use JSON like this:

var obj = {
    name: "abc",
    age: 20
}


new_object=JSON.parse(JSON.stringify(obj));

Now, you have clone of the object obj.

Another solution is like this:

var new_obj={};
for( new_prop in a){
    if (obj.hasOwnProperty(new_prop)){
          new_obj[new_prop]=obj[new_prop]
     }
Source Link
Jack_of_All_Trades
  • 11.6k
  • 20
  • 64
  • 96

Okay, This might be a strange solution but it is plain javascript. If you want to clone something, I would go with the word, "deep copy", you can use JSON like this:

var obj = {
    name: "abc",
    age: 20
}


new_object=JSON.parse(JSON.stringify(obj));

Now, you have clone of the object obj.