最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How do I create an empty copy of an object? - Stack Overflow

programmeradmin1浏览0评论

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
Add a ment  | 

3 Answers 3

Reset to default 6

You 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);
发布评论

评论列表(0)

  1. 暂无评论