Js如何实现对json克隆

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Js如何实现对json克隆相关的知识,希望对你有一定的参考价值。

//数组的克隆
var obj = [1,2,[3,4,5]]; 
var objCloned = JSON.parse(JSON.stringify(obj)); 
console.log(obj); 
console.log(JSON.stringify(obj)); 
console.log(objCloned); 
【代码来源网络】

这种是很简单的克隆。

参考技术A 简单的复制还是什么

js 递归实现深度克隆与浅克隆

直接看代码吧,浅显易懂并配有英文注解,


	/**
	 * Iterate over an Array or an Object invoking a function for each item.
	 *
	 * If `obj` is an Array callback will be called passing
	 * the value, index, and complete array for each item.
	 *
	 * If \'obj\' is an Object callback will be called passing
	 * the value, key, and complete object for each property.
	 *
	 * @param {Object|Array} obj The object to iterate
	 * @param {Function} fn The callback to invoke for each item
	 */
	function forEach(obj, fn) {
	  // Don\'t bother if no value provided
	  if (obj === null || typeof obj === \'undefined\') {
	    return;
	  }
	
	  // Force an array if not already something iterable
	  if (typeof obj !== \'object\') {
	    /*eslint no-param-reassign:0*/
	    obj = [obj];
	  }
	
	  if (isArray(obj)) {
	    // Iterate over array values
	    for (var i = 0, l = obj.length; i < l; i++) {
	      fn.call(null, obj[i], i, obj);
	    }
	  } else {
	    // Iterate over object keys
	    for (var key in obj) {
	      if (Object.prototype.hasOwnProperty.call(obj, key)) {
	        fn.call(null, obj[key], key, obj);
	      }
	    }
	  }
	}
	/**
	 * Accepts varargs expecting each argument to be an object, then
	 * immutably merges the properties of each object and returns result.
	 *
	 * When multiple objects contain the same key the later object in
	 * the arguments list will take precedence.
	 *
	 * Example:
	 *
	 * ```js
	 * var result = merge({foo: 123}, {foo: 456});
	 * console.log(result.foo); // outputs 456
	 * ```
	 *
	 * @param {Object} obj1 Object to merge
	 * @returns {Object} Result of all merge properties
	 */
	function merge(/* obj1, obj2, obj3, ... */) {
	  var result = {};
	  function assignValue(val, key) {
	    if (typeof result[key] === \'object\' && typeof val === \'object\') {
	      result[key] = merge(result[key], val);
	    } else {
	      result[key] = val;
	    }
	  }
	
	  for (var i = 0, l = arguments.length; i < l; i++) {
	    forEach(arguments[i], assignValue);
	  }
	  return result;
	}
	
	/**
	 * Function equal to merge with the difference being that no reference
	 * to original objects is kept.
	 *
	 * @see merge
	 * @param {Object} obj1 Object to merge
	 * @returns {Object} Result of all merge properties
	 */
	function deepMerge(/* obj1, obj2, obj3, ... */) {
	  var result = {};
	  function assignValue(val, key) {
	    if (typeof result[key] === \'object\' && typeof val === \'object\') {
	      result[key] = deepMerge(result[key], val);
	    } else if (typeof val === \'object\') {
	      result[key] = deepMerge({}, val);
	    } else {
	      result[key] = val;
	    }
	  }
	
	  for (var i = 0, l = arguments.length; i < l; i++) {
	    forEach(arguments[i], assignValue);
	  }
	  return result;
	}

代码来源注释:作者在看axios源码时看到的片段。

以上是关于Js如何实现对json克隆的主要内容,如果未能解决你的问题,请参考以下文章

如何创建和克隆 JSON 对象?

关于JavaScript对象深度克隆

如何克隆蒙皮网格?

如何实现对象克隆?

62 如何实现对象克隆?

jQuery - 结合克隆内容和淡入