可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is there an efficient way to clone an object yet leave out specified properties? Ideally without rewriting the $.extend function?
var object = { "foo": "bar" , "bim": Array [1000] }; // extend the object except for the bim property var clone = $.extend({}, object, "bim"); // = { "foo":"bar" }
My goal is to save resources by not copying something I'm not going to use.
回答1:
jQuery.extend
takes an infinite number of arguments, so it's not possible to rewrite it to fit your requested format, without breaking functionality.
You can, however, easily remove the property after extending, using the delete
operator:
var object = { "foo": "bar" , "bim": "baz" }; // extend the object except for the bim property var clone = $.extend({}, object); delete clone.bim; // = { "foo":"bar" }
回答2:
You can archive what you want with either pick or omit from underscore, they both allow to make a copy of an object and filter certain keys from the source object to be included or omitted in the copy/clone:
var object = { "foo": "bar" , "bim": Array [1000] }; // copy the object and omit the desired properties out // _.omit(sorceObject, *keys) where *keys = key names separated by coma or an array of keys var clone = _.omit(object, 'bim'); // { "foo":"bar" }