Javascript 如何在 JQuery 中复制/克隆哈希/对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7113865/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 00:53:58  来源:igfitidea点击:

How to copy/clone a hash/object in JQuery?

javascriptjquerycopyclonejavascript-objects

提问by at.

I have a simple object (or hash) in Javascript:

我在 Javascript 中有一个简单的对象(或哈希):

var settings = {
  link: 'http://example.com',
  photo: 'http://photos.com/me.jpg'
};

I need a copy of it. Is there a settings.clone()type method that will give me another object with the same attributes? I'm using jQuery, so happy to use a jQuery utility method if one exists.

我需要它的副本。有没有一种settings.clone()类型方法可以给我另一个具有相同属性的对象?我正在使用 jQuery,很高兴使用 jQuery 实用程序方法(如果存在)。

回答by pimvdb

Yes, extendan empty object with the original one; that way, everything will simply be copied:

是的,用原始对象扩展一个空对象;这样,一切都将被简单地复制:

var clone = $.extend({}, settings);


Extending some filled object with another, e.g.:

用另一个扩展一些填充对象,例如:

$.extend({a:1}, {b:2})

will return:

将返回:

{a:1, b:2}


With the same logic:

同样的逻辑:

$.extend({}, {foo:'bar', test:123})

will return:

将返回:

{foo:'bar', test:123}

i.e. effectively a clone.

即实际上是一个克隆。

回答by Pantelis

In a non jQuery way.

以非 jQuery 的方式。

var newObj = {};

Object.keys(settings).forEach(function(key) {
     newObj[ key ] = settings[ key ];
}); 

This copies only the top-level properties. To copy hashes with nested objects as property values, you will need to use a recursive function.

这仅复制顶级属性。要将嵌套对象的哈希复制为属性值,您需要使用递归函数。

NB:The Object.keys(settings)avoids the need for calling settings.hasOwnProperty(key).

注:Object.keys(settings)避免调用的需要settings.hasOwnProperty(key)

回答by Rax

var clone = $.extend(true, {}, settings);

Set first argument to true.

将第一个参数设置为 true。

EDIT: First argument truesignifies deep copy. For given example in original question there is no need for deep copy since there are simple immutable key-value pairs. For question in title - as a general case - use deep copy. Otherwise you get half-copy.

编辑:第一个参数true表示深拷贝。对于原始问题中的给定示例,不需要深复制,因为存在简单的不可变键值对。对于标题中的问题 - 作为一般情况 - 使用深拷贝。否则你会得到半份。

回答by fguillen

My 2 cents:

我的 2 美分:

function clone(hash) {
  var json = JSON.stringify(hash);
  var object = JSON.parse(json);

  return object;
}

It may not be the most optimized option but it can be handy for some scenarios.

它可能不是最优化的选项,但在某些情况下可能很方便。

回答by aepheus

It sounds like you want jQuery extend, which can copy an object for you.

听起来您想要 jQuery 扩展,它可以为您复制一个对象。

http://api.jquery.com/jQuery.extend/

http://api.jquery.com/jQuery.extend/

回答by d-coded

Underscore.js also has an extend functionif you are not using jQuery:

如果您不使用 jQuery,Underscore.js也有一个扩展功能

extend_.extend(destination, *sources)Copy all of the properties in the source objects over to the destination object, and return the destination object. It's in-order, so the last source will override properties of the same name in previous arguments.

_.extend({name: 'moe'}, {age: 50});
=> {name: 'moe', age: 50}

_.extend(destination, *sources)源对象中的所有属性复制到目标对象,并返回目标对象。它是有序的,因此最后一个源将覆盖先前参数中的同名属性。

_.extend({name: 'moe'}, {age: 50});
=> {name: 'moe', age: 50}