Bug Tracker

Opened 9 years ago

Closed 9 years ago

#15246 closed bug (notabug)

$.extend with deep copy of an array property merges the array instead of replacing it

Reported by: i23098 Owned by:
Priority: undecided Milestone: None
Component: unfiled Version: 1.11.1
Keywords: Cc:
Blocked by: Blocking:

Description

If I run $.extend(true, {}, {'a': [1,2,3]}, {'a':[4]}) I would expect the result to be {"a":[4]} as property a should be overwritten instead of merged...

http://jsfiddle.net/pm1f3utz/

Change History (2)

comment:1 Changed 9 years ago by i23098

It also happens in 2.x

comment:2 Changed 9 years ago by gibson042

Resolution: notabug
Status: newclosed

Merging objects and arrays rather than overwriting them is the behavior that differentiates our deep extend from our shallow extend.

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

On a deep extend, Object and Array are extended, but object wrappers on primitive types such as String, Boolean, and Number are not. Deep-extending a cyclical data structure will result in an error.

For needs that fall outside of this behavior, write a custom extend method instead, or use a library like lodash.

Note: See TracTickets for help on using tickets.