Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ignore a property while using jquery $.extend()?

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.

like image 394
stinkycheeseman Avatar asked Mar 26 '12 20:03

stinkycheeseman


1 Answers

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" }
like image 195
Rob W Avatar answered Sep 23 '22 01:09

Rob W