Merge loops with jQuery.extend: last value is always the same?

I have two arrays of "hashes". I need to merge them together so the end result is another array of hashes, except the number of entries is the product of the two arrays.

    inhabitants = {}
    idx = 0
    for (i=0; i<persons.length; i++) {
        person = persons[i];
        for (j=0; j<houses.length; j++) {
            house = houses[j];
            console.log(house);
            inhabitants[idx] = $.extend(person,house);
            console.log(inhabitants[idx]);
            idx++;
        }
    }

What I end up with is a silly number of entries that depends on the ordering of the parameters in the $.extend() line. And the "house" entry that is added is ALWAYS THE LAST ENTRY IN THE ARRAY.

Clearly this $.extend() from jQuery is not doing what I expect. Can anyone help?

Answers


$.extend(a, b) merges b into a, and returns a.

The return value you're using but it seems you were missing the fact that a is also being changed - you're currently assigning the same variable person to inhabitants[idx] each time.


Need Your Help

Connecting to Google Talk over XMPP on Node.js

node.js xmpp google-talk

I've tried using a variety of XMPP libraries for Node.js, and am having trouble connecting to Google Talk's XMPP servers. I'm wanting to connect and read the status of friends, but I can't even ge...

How to add a 2nd y-axis for a density plot superimposed on a histogram in R?

r histogram density-plot

I have a histogram shown below, and I have added 2 density plots on top of that. It corresponds to the 2 classes that make up the data.

Why OCaml's threading is considered as `not enough`?

functional-programming ocaml

It seems many people are saying OCaml does not have a good capacity for concurrency and it is also not good for web server applications.