Skip to main content

Bug Tracker

Side navigation

#9477 closed bug (wontfix)

Opened June 01, 2011 06:46AM UTC

Closed June 13, 2011 09:31PM UTC

Last modified June 22, 2011 07:18AM UTC

extend (deep) merges arrays fields instead of replacing them

Reported by: tsvetomir Owned by: tsvetomir
Priority: low Milestone: 1.next
Component: core Version: 1.6.1
Keywords: neededdocs Cc:
Blocked by: Blocking:
Description

A deep extend does not overwrite array fields, but tries to merge them.

Example: [0, 1, 2] and ![3] produce [3, 1, 2] instead of ![3].

[

This behavior is not consistent or expected. If we apply it on strings "abc" and "d" should merge to "dbc".

Possibly related issue: [

Attachments (0)
Change History (13)

Changed June 01, 2011 03:14PM UTC by rwaldron comment:1

component: unfiledcore
owner: → rwaldron
status: newassigned

Can you cite references of what you believe to be the correct behaviour?

Changed June 01, 2011 03:14PM UTC by rwaldron comment:2

owner: rwaldrontsvetomir
status: assignedpending

Changed June 01, 2011 05:21PM UTC by dmethvin comment:3

Yeah, this looks correct to me. Object and Array are extended, but primitive string/boolean/number are not.

Changed June 01, 2011 05:35PM UTC by rwaldron comment:4

That's how I've always understood "extend" functions to behave, hopefully the OP will have references that will "enlighten" us.

Changed June 02, 2011 08:30AM UTC by tsvetomir comment:5

status: pendingnew

We have a defined behavior for extending objects (union), but we shouldn't assume that the user expects the same for sets of data.

Arrays should be treated as sets of data and not like objects. They can be combined in an union, intersection, a complement or in a user-defined way.

These two similar implementations yield the result I'd expect:

Object.extend (Prototype)

http://jsfiddle.net/JV7m8/1/

Object.append (Mootools)

http://jsfiddle.net/dThRz/1/

Changed June 02, 2011 03:38PM UTC by rwaldron comment:6

priority: undecidedlow
resolution: → worksforme
status: newclosed

This yields the results exactly as shown above: http://jsfiddle.net/rwaldron/zkCZt/

Changed June 02, 2011 07:25PM UTC by tsvetomir comment:7

extend(a, b) vs. extend(true, a, b)

You need to supply the true (deep) parameter to reproduce the issue. Otherwise you're overwriting the "nested" object http://jsfiddle.net/MN5xQ/

Changed June 02, 2011 07:50PM UTC by tsvetomir comment:8

p.s. Turns out Prototype and Mootools don't have a true deep extend. They always overwrite nested objects and arrays. Sorry for comparing apples to oranges!

Either way, I'm not the only one confused by this behavior.

See http://forum.jquery.com/topic/jquery-extend-modifies-but-not-replaces-array-properties-on-deep-copy

Changed June 13, 2011 10:14AM UTC by Ariel <asjquery@dsgml.com> comment:9

I just ran into this.

See: http://jsfiddle.net/s5jNF/

I would expect Arrays to be overwritten or appended. But don't replace the individual elements piecemeal, which is what happens now.

Personally I need a deep extend where the array elements are appended. (Although I can see cases where I would want them overwritten.)

Can we get this as an option?

rwaldron, please reopen this. It's not worksforsome - it's wontfix or it's open.

Changed June 13, 2011 02:47PM UTC by rwaldron comment:10

resolution: worksforme
status: closedreopened

Changed June 13, 2011 08:49PM UTC by dmethvin comment:11

Not sure why it can't be closed worksforme with needsdocs. The proposed changes might break existing code, which would be one reason to avoid changing it. For specific needs it is no doubt faster (performance-wise) to write a custom extend method, our poor method is already overburdened with constraints.

Changed June 13, 2011 09:31PM UTC by rwaldron comment:12

description: A deep '''extend''' does not overwrite array fields, but tries to merge them. \ \ Example: [0, 1, 2] and ![3] produce [3, 1, 2] instead of ![3]. \ [http://jsfiddle.net/9xBw4/] \ \ This behavior is not consistent or expected. If we apply it on strings "abc" and "d" should merge to "dbc". \ \ Possibly related issue: [http://bugs.jquery.com/ticket/6958]A deep '''extend''' does not overwrite array fields, but tries to merge them. \ \ Example: [0, 1, 2] and ![3] produce [3, 1, 2] instead of ![3]. \ [ \ \ This behavior is not consistent or expected. If we apply it on strings "abc" and "d" should merge to "dbc". \ \ Possibly related issue: [
keywords: → needsdocs
resolution: → wontfix
status: reopenedclosed

Changed June 22, 2011 07:18AM UTC by addyosmani comment:13

keywords: needsdocsneededdocs