10

I have a problem concatenating two associative arrays in JavaScript. Below is the sample code:

var firstArray =  new Array();
firstArray.c1 = "sam";
firstArray.c2 = "kam";
var secArray =  new Array();
secArray.c3 = "sam";
secArray.c4 = "kam";
var res = firstArray.concat(secArray);

Is this a known limitation?

What's the best way to achieve this?

7
  • wow.... may i know why the heck i am downvoted........ asking question is wrong here..?? pls show ur face ur friend....... Commented Aug 21, 2009 at 8:11
  • 1
    I didn't vote you down, but I guess it would be something to do with the fact that you don't really explain the problem you are seeing Commented Aug 21, 2009 at 8:27
  • brother i gave all the info i got... (concatenating two associative arrays ).. isnt that enough... ppl ask questions because they dont know.... right.. Commented Aug 21, 2009 at 8:32
  • 3
    @Ramesh: you didn't show what the result was. You didn't show what result you hoped to achieve. Commented Aug 21, 2009 at 8:33
  • 3
    Ramesh, this kind of questions needs: a) What you tried, b) What you expected to happen and c) What actaully happened. You didn't supply b) and c) Commented Aug 21, 2009 at 15:48

5 Answers 5

21

You are not using Array functionality - just Object functionality. In JavaScript, Object is an associative array - you use Array for arrays indexed by integers. If you did

var firstArray =  new Array();
firstArray.push("sam");  
firstArray.push("kam");
var secArray =  new Array();
secArray.push("sam");    
secArray.push("kam");
var res = firstArray.concat(secArray);

then concat would work as expected.

If you actually want to merge associative arrays, do:

for (var attr in src_array) { dest_array[attr] = src_array[attr]; }

This will of course overwrite existing keys in dest_array which have counterparts in src_array.

Sign up to request clarification or add additional context in comments.

Comments

3

Try this:

var firstArray = new Array("sam", "kam");
var secArray = new Array("sam", "kam");
var res = firstArray.concat(secArray);

Comments

1

Arrays in JavaScript have only numerical keys. Only objects can have non numerical properties. So try this instead:

var firstArray = {};
firstArray.c1 = "sam";
firstArray.c2 = "kam";
var secArray =  {};
secArray.c3 = "sam";  
secArray.c4 = "kam";

for (var prop in secArray) {
    if (secArray.hasOwnProperty(prop)) {
        firstArray[prop] = secArray[prop];
    }
}

1 Comment

Why the redundancy? You're looping over the properties in the second array and then calling "hasOwnProperty" on the second array with the value from the first loop. Did you mean to test "hasOwnProperty" on the first array?
1

JavaScript doesn't have associative arrays; it has object hashes. You're creating an array and assigning values to some of its properties, not in the array itself.

Your concat will not work because the values are object properties. To do a concat the way you have it, you'll need to combine the two objects. YUI, jQuery, and the other JavaScript frameworks provide helpful methods to do just that.

Comments

1

Strictly speaking, those aren't associative arrays at all: they are arrays of zero length, with additional named properties. Assigning those properties works because arrays are also objects in JavaScript; but that doesn't make them an associative array. It's better to look at them as hashes.

Array methods such as concat will only work with the numerically-indexed elements of arrays, not with the properties of objects - even if those objects happen to be arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.