ES5 question: merging two objects together?

3,106 views
Skip to first unread message

James Carr

unread,
Jul 24, 2010, 1:46:18 AM7/24/10
to nod...@googlegroups.com
I keep thinking there was some method available to combine the
properties of two objects, but it eludes me. Something like:

merge({a:2, b:3}, {c:3}) == {a:2, b:3, c:3}

Any ideas?

Thanks,
James

Stephen Belanger

unread,
Jul 24, 2010, 1:53:05 AM7/24/10
to nod...@googlegroups.com
Not exactly node-related, but try this.

var merge = function(a, b) {
    for (var i in b) {
        a[i] = b[i];
    }
    return a;
}


--
You received this message because you are subscribed to the Google Groups "nodejs" group.
To post to this group, send email to nod...@googlegroups.com.
To unsubscribe from this group, send email to nodejs+un...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/nodejs?hl=en.


Dean Landolt

unread,
Jul 24, 2010, 1:55:02 AM7/24/10
to nod...@googlegroups.com
On Sat, Jul 24, 2010 at 1:53 AM, Stephen Belanger <cyruz...@gmail.com> wrote:
Not exactly node-related, but try this.

var merge = function(a, b) {
    for (var i in b) {
        a[i] = b[i];
    }
    return a;
}

This mutates a -- you'd probably want to create a new object here and loop through both a and b to avoid any surprises. 

James Carr

unread,
Jul 24, 2010, 2:01:05 AM7/24/10
to nod...@googlegroups.com
Yeah, I know of ways to do this by hand, I was just under the
impression that ES5 had a built-in function for it. Am I incorrect?
I'm looking at the spec now, but so far no sign.

Thanks,
James

Stephen Belanger

unread,
Jul 24, 2010, 2:02:30 AM7/24/10
to nod...@googlegroups.com
It's just a basic example. Personally I'd make something a little more robust, that iterates until arguments.length, so you could do fancy stuff like merge({a,1}, {b:2}, {c:3});

Stephen Belanger

unread,
Jul 24, 2010, 2:06:12 AM7/24/10
to nod...@googlegroups.com
I don't recall if there is anything built-in for it, as I've always just made my own. Usually the built-in stuff isn't quite as flexible as it could be, just to be as generic as possible. Thus, if you plan on doing anything particularly complex it's often best to just do it yourself. Then you know exactly how it's "supposed" to function, and won't encounter any unpleasant surprises later on.

Marco Rogers

unread,
Jul 24, 2010, 9:04:53 AM7/24/10
to nodejs
No built in function. I use this. It takes into account variable
arguments and accessor methods.

http://github.com/polotek/evented-twitter/blob/master/lib/underscore.js#L443-461


On Jul 24, 2:06 am, Stephen Belanger <cyruzdr...@gmail.com> wrote:
> I don't recall if there is anything built-in for it, as I've always just
> made my own. Usually the built-in stuff isn't quite as flexible as it *could
> * be, just to be as generic as possible. Thus, if you plan on doing anything
> particularly complex it's often best to just do it yourself. Then you know
> exactly how it's "supposed" to function, and won't encounter any unpleasant
> surprises later on.
>
>
>
> On Fri, Jul 23, 2010 at 11:01 PM, James Carr <james.r.c...@gmail.com> wrote:
> > Yeah, I know of ways to do this by hand, I was just under the
> > impression that ES5 had a built-in function for it. Am I incorrect?
> > I'm looking at the spec now, but so far no sign.
>
> > Thanks,
> > James
>
> > On Sat, Jul 24, 2010 at 12:55 AM, Dean Landolt <d...@deanlandolt.com>
> > wrote:
>
> > > On Sat, Jul 24, 2010 at 1:53 AM, Stephen Belanger <cyruzdr...@gmail.com>
> > > wrote:
>
> > >> Not exactly node-related, but try this.
> > >> var merge = function(a, b) {
> > >>     for (var i in b) {
> > >>         a[i] = b[i];
> > >>     }
> > >>     return a;
> > >> }
>
> > > This mutates a -- you'd probably want to create a new object here and
> > loop
> > > through both a and b to avoid any surprises.
>
> > > --
> > > You received this message because you are subscribed to the Google Groups
> > > "nodejs" group.
> > > To post to this group, send email to nod...@googlegroups.com.
> > > To unsubscribe from this group, send email to
> > > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .
> > > For more options, visit this group at
> > >http://groups.google.com/group/nodejs?hl=en.
>
> > --
> > You received this message because you are subscribed to the Google Groups
> > "nodejs" group.
> > To post to this group, send email to nod...@googlegroups.com.
> > To unsubscribe from this group, send email to
> > nodejs+un...@googlegroups.com<nodejs%2Bunsu...@googlegroups.com>
> > .
Reply all
Reply to author
Forward
0 new messages