在 javascript 中克隆一个对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5364650/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cloning an object in javascript
提问by Matrym
The below first logs 0, and then logs 1. How do I store a copy of the object, rather than a reference to it?
下面首先记录 0,然后记录 1。如何存储对象的副本,而不是对它的引用?
debug.log(vi.details.segment);
vi.nextSegment = vi.details;
vi.nextSegment.segment++;
debug.log(vi.details.segment);
回答by Mike Lewis
To clone an object in jQuery:
在 jQuery 中克隆一个对象:
var vi.nextSegment = jQuery.extend({}, vi.details);
NOTE:The above is a shallow copy: any nested objects or arrays will be copied by reference - meaning any changes you make to vi.nextSegment.obj[prop]
will be reflected in vi.details.obj[prop]
. If you want a completely new object which is completely separate from the original, you will need to do a deep copy (pass true
as the first parameter):
注意:以上是浅拷贝:任何嵌套的对象或数组都将通过引用复制 - 这意味着您所做的任何更改vi.nextSegment.obj[prop]
都将反映在vi.details.obj[prop]
. 如果您想要一个与原始对象完全分离的全新对象,则需要进行深度复制(true
作为第一个参数传递):
var vi.nextSegment = jQuery.extend(true, {}, vi.details);
To read up more on extend, see here.
要阅读有关扩展的更多信息,请参见此处。
回答by Scoobler
Take a look at the post: What is the most efficient way to clone a javascript object
看一下帖子:克隆javascript对象的最有效方法是什么
As per John Resig'sanswer:
根据John Resig 的回答:
// Shallow copy
var newObject = jQuery.extend({}, oldObject);
// Deep copy
var newObject = jQuery.extend(true, {}, oldObject);
More information can be found in the jQuery documentation.
更多信息可以在jQuery 文档中找到。
回答by Mecalito
This worked better for me cloning an object using jQuery "parseJSON()" and "JSON.stringify()"
这对我使用 jQuery“parseJSON()”和“JSON.stringify()”克隆对象效果更好
$.ajax({
url: 'ajax/test.html',
dataType: 'json',
success: function(data) {
var objY = $.parseJSON(JSON.stringify(data));
var objX = $.parseJSON(JSON.stringify(data));
}
});
Cloning data object in objX & objY are two different object, you do no have to mess up with the "by reference" problem
objX 和 objY 中的克隆数据对象是两个不同的对象,您不必纠结于“按引用”问题
Gracias!
谢谢!
回答by MarkosyanArtur
Another way to clone object is
克隆对象的另一种方法是
newObj = JSON.parse(JSON.stringify(oldObj));
But be careful if it's contains dates. JSON.parse will return date.toString() instead of date in that case.
但如果它包含日期,请小心。在这种情况下,JSON.parse 将返回 date.toString() 而不是 date。
回答by Francisco Corrales Morales
This is how I copy elements several times:
这就是我多次复制元素的方式:
First I have a template:
首先我有一个模板:
<div class="forms-container">
<div class="form-template">
First Name <input>
.. a lot of other data ...
Last Name <input>
<div>
<button onclick="add_another();">Add another!</button>
<div>
Now, the JavaScript:
现在,JavaScript:
function add_another(){
jQuery(".form-template").clone().appendTo(".forms-container");
}
回答by Akash
Try Immutable.js
:
尝试Immutable.js
:
Since jQuery
mostly deals with DOM Elements
, it may notbe the right tool for the job. Immutable.js
is a 56 kb (minified)
library created by Facebook
.
由于jQuery
主要处理DOM Elements
,它可能不是适合这项工作的工具。 Immutable.js
是一个56 kb (minified)
由Facebook
.
// roughly implementing
import Immutable from 'immutable'
//
const oldObj = { foo: 'bar', bar: 'baz' }
// create a map from the oldObj and then convert it to JS Object
const newObj = Immutable.Map(oldObj).toJS()
This way you would have effectively cloned newObj
from oldObj
. Basically, if you don't have a Map
already, then we need to create a Map
first. Map is like a blue-print
that we work with to create copies
.
这样你就可以有效地newObj
从oldObj
. 基本上,如果您还没有Map
,那么我们需要先创建一个Map
。Map 就像是blue-print
我们用来创建copies
.
References :
参考 :
Home -Immutable
首页 -不可变
Docs -Immutable Docs
文档 -不可变文档
GitHub -Immutable@GitHub
GitHub -不可变@GitHub
Good Luck.
祝你好运。