Bug Tracker

Opened 6 years ago

Closed 6 years ago

Last modified 6 years ago

#9477 closed bug (wontfix)

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 (last modified by rwaldron)

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: [

Change History (13)

comment:1 Changed 6 years ago by rwaldron

  • Component changed from unfiled to core
  • Owner set to rwaldron
  • Status changed from new to assigned

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

comment:2 Changed 6 years ago by rwaldron

  • Owner changed from rwaldron to tsvetomir
  • Status changed from assigned to pending

comment:3 Changed 6 years ago by dmethvin

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

comment:4 Changed 6 years ago by rwaldron

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

comment:5 Changed 6 years ago by tsvetomir

  • Status changed from pending to new

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/

comment:6 Changed 6 years ago by rwaldron

  • Priority changed from undecided to low
  • Resolution set to worksforme
  • Status changed from new to closed

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

comment:7 Changed 6 years ago by tsvetomir

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/

comment:8 Changed 6 years ago by tsvetomir

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

comment:9 Changed 6 years ago by Ariel <asjquery@…>

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.

comment:10 Changed 6 years ago by rwaldron

  • Resolution worksforme deleted
  • Status changed from closed to reopened

comment:11 Changed 6 years ago by dmethvin

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.

comment:12 Changed 6 years ago by rwaldron

  • Description modified (diff)
  • Keywords needsdocs added
  • Resolution set to wontfix
  • Status changed from reopened to closed

comment:13 Changed 6 years ago by addyosmani

  • Keywords neededdocs added; needsdocs removed
Note: See TracTickets for help on using tickets.