I am creating some objects from JSON and I would like to make an empty copy of an object. I would have all the same properties but empty values. What are some good ways of doing this?
Right now I am doing it as follows but would like it to be dynamic from the objects I create from the JSON I receive.
var myObject = { "Address": { "Address1": "", "Address2": "", "Address3": "", "Address4": "", "City": "", "": "", "": "", "Country": "", "Id": -1, "LastModified": "", "PostBackAction": null }, "Id": -1, "Amenities": "", "Directions": "", "LastModified": "", "LocalAttractions": "", "LocalLodging": "", "LocalRestaraunts": "", "Name": "", "Pictures": "", "Prices": "", "Region": 0, "PostBackAction": null };
A possible solution that doesn't work because it copies the values.
var myObject = JSON.parse(JSON.stringify(objectToBeCopied));
I am creating some objects from JSON and I would like to make an empty copy of an object. I would have all the same properties but empty values. What are some good ways of doing this?
Right now I am doing it as follows but would like it to be dynamic from the objects I create from the JSON I receive.
var myObject = { "Address": { "Address1": "", "Address2": "", "Address3": "", "Address4": "", "City": "", "": "", "": "", "Country": "", "Id": -1, "LastModified": "", "PostBackAction": null }, "Id": -1, "Amenities": "", "Directions": "", "LastModified": "", "LocalAttractions": "", "LocalLodging": "", "LocalRestaraunts": "", "Name": "", "Pictures": "", "Prices": "", "Region": 0, "PostBackAction": null };
A possible solution that doesn't work because it copies the values.
var myObject = JSON.parse(JSON.stringify(objectToBeCopied));
Share
Improve this question
asked Jul 9, 2014 at 20:43
user3648646user3648646
7112 gold badges16 silver badges24 bronze badges
3
- What do you want your output to be? – Codeman Commented Jul 9, 2014 at 20:44
-
What do you consider empty?
{Address:undefined}
? – Bergi Commented Jul 9, 2014 at 20:57 - What do you need this for? Why don't you have an empty object and copy it for creating objects? – Bergi Commented Jul 9, 2014 at 21:06
3 Answers
Reset to default 6You could use a function that creates a copy of the object structure and uses a default value for each data type:
function skeleton(source, isArray) {
var o = Array.isArray(source) ? [] : {};
for (var key in source) {
if (source.hasOwnProperty(key)) {
var t = typeof source[key];
o[key] = t == 'object' ? skeleton(source[key]) : { string: '', number: 0, boolean: false }[t];
}
}
return o;
}
Demo: http://jsfiddle/Guffa/ym6ZJ/
var getCopy = function(objectToCopy){
var copy = {};
for(var prop in myObject){
if(typeof(objectToCopy[prop])==="object"){
copy[prop]= getCopy(objectToCopy[prop]);
}
else{
copy[prop]=null;
}
}
return copy;
}
Hope this is what you were looking for:
var myObject = function(){
this.address = {
'Address1': '',
'Address2': '',
'Address3': '',
'Address4': '',
'City': '',
'Country': '',
'Id': 0,
'LastModified': '',
'PostBackAction': null
};
this.Id = -1;
this.Amenities = '';
this.Directions = '';
this.LastModified = '';
this.LocalAttractions = '';
this.LocalLodging = '';
this.LocalRestaraunts = '';
this.Name = '';
this.Pictures = '';
this.Prices = '';
this.Region = 0;
this.PostBackAction = null;
}
// create two new objects
var objectOne = new myObject();
var objectTwo = new myObject();
console.log(objectOne);
//modify the first
objectOne.address.Address1 = 'Lol Street';
console.log(objectOne);
//notice the second one was not changed
console.log(objectTwo);