YouTip LogoYouTip

Misc Extend

# jQuery.extend() Method \n\n[![Image 4: jQuery Miscellaneous Methods](#)jQuery Miscellaneous Methods](#)\n\n## Example\n\nIterate through array elements and modify the first object\n\n
$(function(){var object1 = {apple: 0, banana: {weight: 52, price: 100}, cherry: 97}; var object2 = {banana: {price: 200}, durian: 100}; /* object2 Merge into object1 in */ $.extend(object1, object2); var printObj = typeof JSON != "undefined" ? JSON.stringify : function(obj){var arr = []; $.each(obj, function(key, val){var next = key + ": "; next += $.isPlainObject(val) ? printObj(val) : val; arr.push(next); }); return"{ " + arr.join(", ") + " }"; }; $("#log").append(printObj(object1)); })\n\n[Try it Β»](#)\n\n* * *\n\n## Definition and Usage\n\nThe jQuery.extend() function is used to merge the contents of one or more objects into a target object.\n\n**Note:** 1. If only one parameter is specified for $.extend(), it means the parameter target is omitted. In this case, target is the jQuery object itself. Through this method, we can add new functions to the global jQuery object.\n\n2. If multiple objects have the same property, the latter will override the former's property value.\n\n* * *\n\n## Syntax\n\n_$_.extend( target [, object1 ] [, objectN ] )\n\nIndicates whether to perform a deep merge\n\n_$_.extend( , target, object1 [, objectN ] )\n\n**Warning:** Passing false as the first parameter is not supported.\n\n| Parameter | Description |\n| :--- | :--- |\n| _deep_ | Optional. Boolean type. Indicates whether to perform a deep merge of objects, defaults to false. If this value is true, and multiple objects have the same property name that are also objects, the properties of that "property object" will also be merged. |\n| _target_ | Object type. The target object, to which the member properties of other objects will be appended. |\n| _object1_ | Optional. Object type. The first object to be merged. |\n| _objectN_ | Optional. Object type. The Nth object to be merged. |\n\n* * *
← Misc InarrayEvent_Metakey β†’