你如何在Javascript中克隆一个对象数组? [英] How do you clone an Array of Objects in Javascript?

查看:106
本文介绍了你如何在Javascript中克隆一个对象数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

...每个对象还引用同一个数组中的其他对象?

...where each object also has references to other objects within the same array?

当我第一次想出这个问题时,我只是喜欢

When I first came up with this problem I just though of something like

var clonedNodesArray = nodesArray.clone()

将存在并搜索有关如何在javascript中克隆对象的信息。我找到了一个问题 StackOverflow(由同样的@JohnResig回答)他指出用jQuery你可以做到

would exist and searched for info on how to clone objects in javascript. I did find a question on StackOverflow (answered by the very same @JohnResig) and he pointed out that with jQuery you could do

var clonedNodesArray = jQuery.extend({}, nodesArray);

克隆一个对象。我试过这个,但这只复制了数组中对象的引用。所以如果我

to clone an object. I tried this though, this only copies the references of the objects in the array. So if I

nodesArray[0].value = "red"
clonedNodesArray[0].value = "green"

nodesArray [0]和clonedNodesArray [0]的值都将变成绿色 。然后我试了

the value of both nodesArray[0] and clonedNodesArray[0] will turn out to be "green". Then I tried

var clonedNodesArray = jQuery.extend(true, {}, nodesArray);

深度复制一个Object,但我得到了太多的递归分别来自Firebug和Opera Dragonfly的控制堆栈溢出消息。

which deep copies an Object, but I got "too much recursion" and "control stack overflow" messages from both Firebug and Opera Dragonfly respectively.

你会怎么做?这是不应该做的事吗?在Javascript中是否有可重复使用的方法?

How would you do it? Is this something that shouldn't even be done? Is there a reusable way of doing this in Javascript?

推荐答案

浅拷贝的问题是所有对象都不是克隆。虽然每个对象的引用在每个数组中都是唯一的,但是一旦你最终抓住它,你就会像以前一样处理同一个对象。克隆它的方式没有任何问题......使用Array.slice()会产生相同的结果。

The issue with your shallow copy is that all the objects aren't cloned. While the references to each object are unique in each array, once you ultimately grab onto it you're dealing with the same object as before. There is nothing wrong with the way you cloned it... the same result would occur using Array.slice().

深层拷贝出现问题的原因是因为你最终得到了圆形对象引用。 Deep会尽可能地深入,如果你有一个圆圈,它会一直无限地走向浏览器晕倒。

The reason your deep copy is having problems is because you're ending up with circular object references. Deep will go as deep as it can go, and if you've got a circle, it'll keep going infinitely until the browser faints.

如果数据结构不能被表示为有向无环图,那么我不确定你是否能够找到一种用于深度克隆的通用方法。循环图提供了许多棘手的极端情况,因为它不是常见的操作,我怀疑是否有人编写了完整的解决方案(如果它甚至可能 - 它可能不是!但我现在没有时间尝试编写严格的证据。)。我在此页上找到了一些关于此问题的好评。

If the data structure cannot be represented as a directed acyclic graph, then I'm not sure you're going to be able to find an all-purpose method for deep cloning. Cyclic graphs provide many tricky corner cases, and since it's not a common operation I doubt anyone has written a full solution (if it's even possible - it might not be! But I have no time to try to write a rigorous proof now.). I found some good comments on the issue on this page.

如果您需要带有循环引用的对象数组的深层副本,我相信您将需要编写自己的方法来处理您的专用数据结构,这样它就是一个多遍clone:

If you need a deep copy of an Array of Objects with circular references I believe you're going to have to code your own method to handle your specialized data structure, such that it is a multi-pass clone:


  1. 在第一轮中,克隆所有不引用数组中其他对象的对象。跟踪每个对象的来源。

  2. 在第二轮中,将对象链接在一起。

这篇关于你如何在Javascript中克隆一个对象数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆