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

SELECT * FROM help - with 2 variable options

php mysql

Ok, so I found a question in this post here:

error message in qt designer in Linux

linux qt cross-platform

while running an application written in windows on linux, i encountered a problem-

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.