This question already has an answer here: What is the most efficient way to deep clone an object in JavaScript? 57 answers You have two main options: Use JSON.stringify and JSON.parse : var copy = JSON.parse(JSON.stringify(original)); But I've never liked that. A round-trip through text is inefficient at best, and it won't handle Date , RegExp , undefined , etc. values correctly
这个问题在这里已经有了答案: 在JavaScript中深入克隆对象的最有效方法是什么? 57个答案 你有两个主要选择: 使用JSON.stringify和JSON.parse : var copy = JSON.parse(JSON.stringify(original)); 但我从来不喜欢那样。 往返文本效率低下,除非您编写替代者和reviver,否则它不会正确处理Date , RegExp , undefined等值。 使用递归函数,如下所示: var toString = Object.prototype.toString; function deep
Possible Duplicate: What is the most efficient way to clone a JavaScript object? How to clone js object with out reference like these: { ID: _docEl, Index: next, DocName: _el } Any ideas? You'll have to iterate over the object and make copies of all its properties. And then if any of its properties are also objects, assuming you want to clone those too, you'll have to recurs
可能重复: 什么是克隆JavaScript对象最有效的方法? 如何用这样的引用克隆js对象: { ID: _docEl, Index: next, DocName: _el } 有任何想法吗? 您必须遍历该对象并复制其所有属性。 然后,如果它的任何属性也是对象,假设你也想克隆它们,那么就必须缓存它们。 这里有多种方法可以做到这一点:克隆JavaScript对象的最有效方法是什么? 基于thomasrutter的建议(未经测试的代码),我将如何做到这一点: functi
Possible Duplicate: What is the most efficient way to clone a JavaScript object? I have an object like this: User = { name: "user", settings: { first: "1", second: "2" } } and a second one: user1 = { name: "user1", settings: { second: "3" } } now I want to copy user1's custom values into User, using: for(var key in user1){ User[key] = u
可能重复: 什么是克隆JavaScript对象最有效的方法? 我有这样一个对象: User = { name: "user", settings: { first: "1", second: "2" } } 和第二个: user1 = { name: "user1", settings: { second: "3" } } 现在我想将user1的自定义值复制到User中,使用: for(var key in user1){ User[key] = user1[key]; } 结果用户将是: User = { name: "user1", se
Possible Duplicate: What is the most efficient way to clone a JavaScript object? This is also referred to as "deep copying", which I've found some articles on. Closest seems to be this one but it's for jQuery - I'm trying to do this without a library. I've also seen, in two places, that it's possible to do something like: arr2 = JSON.decode(JSON.encode(arr1))
可能重复: 什么是克隆JavaScript对象最有效的方法? 这也被称为“深层复制”,我发现了一些文章。 最近似乎是这一个,但它是为jQuery - 我试图做到这一点,没有一个库。 我也在两个地方看到,有可能做到这样的事情: arr2 = JSON.decode(JSON.encode(arr1)); 但是这显然效率很低。 也可以单独循环和复制每个值,并遍历所有数组。 这看起来很累,效率也不高。 那么,复制JavaScript多维数组[[a],[b],[c]]的最高效,
I have a big object with much data. And i want to clone this in other variable. When i set some param of the instance B has the same result in the original object: var obj = {a: 25, b: 50, c: 75}; var A = obj; var B = obj; A.a = 30; B.a = 40; alert(obj.a + " " + A.a + " " + B.a); // 40 40 40 My output should be 25 30 40. Any ideas? EDIT Thanks Everyone. I change the code of dystroy and
我有一个拥有大量数据的大对象。 我想在其他变量中克隆它。 当我设置实例B的一些参数在原始对象中具有相同的结果时: var obj = {a: 25, b: 50, c: 75}; var A = obj; var B = obj; A.a = 30; B.a = 40; alert(obj.a + " " + A.a + " " + B.a); // 40 40 40 我的输出应该是25 30 40.任何想法? 编辑 感谢大家。 我改变了dystroy的代码,这是我的结果: Object.prototype.clone = Array.prototype.clone = function()
This question already has an answer here: What is the most efficient way to deep clone an object in JavaScript? 57 answers 我发现如果你不使用jQuery,只对克隆简单对象感兴趣,请参阅以下内容(请参阅注释)。 JSON.parse(JSON.stringify(json_original)); Your only option is to somehow clone the object. See this stackoverflow question on how you can achieve this. For simple JSON objects, the simp
这个问题在这里已经有了答案: 在JavaScript中深入克隆对象的最有效方法是什么? 57个答案 我发现如果你不使用jQuery,只对克隆简单对象感兴趣,请参阅以下内容(请参阅注释)。 JSON.parse(JSON.stringify(json_original)); 你唯一的选择是以某种方式克隆该对象。 关于如何实现这一点,请参阅此stackoverflow问题。 对于简单的JSON对象,最简单的方法是: var newObject = JSON.parse(JSON.stringify(oldObject));
Possible Duplicate: What is the most efficient way to clone a JavaScript object? I need to copy an (ordered, not associative) array of objects. I'm using jQuery. I initially tried jquery.extend({}, myArray) but, naturally, this gives me back an object, where I need an array (really love jquery.extend, by the way). So, what's the best way to copy an array? Since Array.slice() d
可能重复: 什么是克隆JavaScript对象最有效的方法? 我需要复制一个(有序的,不关联的)对象数组。 我正在使用jQuery。 我最初尝试过 jquery.extend({}, myArray) 但是,自然,这给了我一个对象,我需要一个数组(真的很喜欢jquery.extend,顺便说一下)。 那么,复制数组的最佳方式是什么? 由于Array.slice()不执行深度复制,因此它不适用于多维数组: var a =[[1], [2], [3]]; var b = a.slice(); b.shift()
I have been confused over when to use these two parsing methods. After I echo my json_encoded data and retrieve it back via ajax, I often run into confusion about when I should use JSON.stringify and JSON.parse . I get [object,object] in my console.log when parsed and a JavaScript object when stringified. $.ajax({ url: "demo_test.txt", success: function(data) { console.log(JSON.stri
我一直困惑于何时使用这两种解析方法。 在我回应我的json_encoded数据并通过ajax取回之后,我经常会对我应该何时使用JSON.stringify和JSON.parse感到困惑。 我的console.log在解析时得到了[object,object] ,而在字符串化时得到了一个JavaScript对象。 $.ajax({ url: "demo_test.txt", success: function(data) { console.log(JSON.stringify(data)) /* OR */ console.log(JSON.pars
I have an object that prints the mouse's x and y positions on every mousemove. It's something like this: $('#canvas').mousemove(function(e){ $('#output').prepend(e.pageX + ',' + e.pageY); }); I've noticed that when you move over the object really fast it only prints out a few positions. I'm not exactly unhappy that it does that (because it would be quite exhaustive to ha
我有一个对象在每个鼠标移动时打印鼠标的x和y位置。 这是这样的: $('#canvas').mousemove(function(e){ $('#output').prepend(e.pageX + ',' + e.pageY); }); 我注意到,当你快速移动对象时,它只打印出几个位置。 我并不是完全不满意这样做(因为要让它为所有数百个像素做点什么都是非常详尽的),但我想知道它是如何工作的。 mousemove事件是否限于每秒一定数量的触发器或什么? (顺便说一句:这是在Ubuntu Li
The Map I'm making a tile based RPG with Javascript, using perlin noise heightmaps, then assigning a tile type based on the height of the noise. The maps end up looking something like this (in the minimap view). I have a fairly simple algorithm which extracts the color value from each pixel on the image and converts it into a integer (0-5) depending on its postion between (0-255) which
地图 我正在使用Javascript制作基于图块的RPG游戏,使用perlin噪声高度图,然后根据噪音高度分配图块类型。 地图最终看起来像这样(在小地图视图中)。 我有一个相当简单的算法,它从图像上的每个像素中提取颜色值,并根据其与瓷砖字典中的图块对应的(0-255)之间的位置将其转换为整数(0-5)。 然后将这个200x200数组传递给客户端。 然后引擎根据数组中的值确定图块并将其绘制到画布上。 所以,我最终会看到有趣的世