如何克隆 object 并将每个属性值设置为空?

How can I clone an object and set every properties value to null?

根据标题,我需要像下面那样在 Javascript 中克隆一个 object,并将每个值设置为零。当然 object 属性可以更改。

  { _id: { action: null, date: null },
    avg: null,
    min: null,
    max: null,
    total: null }
// helper method to get the correct object type
function toType(x) {
  return ({}).toString.call(x).match(/\s([a-zA-Z]+)/)[1].toLowerCase();
}

// recursive function that sets all properties to null
// except objects which it passes back into the reset function
function reset(obj) {

  // clone the object
  var out = JSON.parse(JSON.stringify(obj));
  for (var p in out) {
    if (toType(out[p]) === 'object') {
      reset(out[p]);
    } else {
      out[p] = null;
    }
  }
  return out;
}

reset(obj);

DEMO