2

I have this array,

var arr1 = [19, 1, 1, 1, 1];

and another array,

var arr2 = ["Default", "Dynamic", "Assessment", "Risk", "Privacy"];

What I would like is to merge them somehow such that it would look something like this,

var merged_array = [ ["Default", 19], ["Dynamic", 1], ["Assessment", 1], ["Risk", 3], ["Privacy", 2] ];

how would I do that? I tried join but I did not achieve what I wanted. Thanks in advance!

5
  • Do you know how a for loop works? Try that. Commented Jan 17, 2013 at 22:08
  • There is no convenience method in vanilla javascript. You have to get the greater length of bot array, then loop over bot, check if there still is an entry for that index, create a sub arry, get the elements from bot arrays into that, add it to a new third array. Voila. Commented Jan 17, 2013 at 22:11
  • @FritsvanCampen, I have not tried using for, I never thought of using that one either. But I'll try that later. Cheers! Commented Jan 17, 2013 at 22:11
  • In some libraries, e.g. Prototype.js, there is a zip() function that does this. Commented Jan 17, 2013 at 22:11
  • @FritsvanCampen, I actually used for(), thanks for the tip! :) Commented Jan 17, 2013 at 22:26

5 Answers 5

1

Using a for loop:

var merged_array = new Array(arr1.length);
for (var i = 0; i < arr1.length; i++) {
  merged_array[i] = new Array(arr2[i], arr1[i]);
}

This assumes arr1 and arr2 have the same length.

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

3 Comments

not too good with javascript but are you missing a 'new' statement?
arr1.length and arr2.length are of both the same.
@user814628: They both work, but I suppose having new is better. Changed my code. Thanks.
1

If you have jquery or equivalent:

var merged_array = $.map(arr1, function(e, i) {
    return [arr2[i], e];
});

If not, then just use a for loop, same idea.

var merged_array = []
for (var i = 0; i < arr1.length && i < arr2.length; i++) {
    merged_array[i] = [arr2[i], arr1[i]];
}

Comments

1
var result = new Array();
for (var i=0; i<arr1 .length && i<arr2.length ; i++) {
    result[i] = new Array();
    result[i][0] = arr1[i];
    result[i][1] = arr2[i];
}

Comments

1

I'd probably extend the array object especially if this is a common task. Something like this:

Array.prototype.myMerge = function(arr){
    var returnArr = [];
    for(var i = 0, len = this.length; i < len; i++){
        returnArr[i] = [this[i], arr[i]];
    }
    return returnArr;
};

then you could call it like this:

var merged_array = arr1.myMerge(arr2)

Of course you'd have to add some error checking on the length of the arrays, this function assumes that arr1 is longer or the same length as arr2. But that depends on what you want to do if arr1 and arr2 are different lengths, your question seems to assume they are the same length.

Comments

1

If you include the library underscore.js (prototype.js also has something similar), you can use _.zip(array1,array2). They provide example usage with 3 arrays.

From http://underscorejs.org/#zip :

zip_.zip(*arrays) Merges together the values of each of the arrays with the values at the corresponding position. Useful when you have separate data sources that are coordinated through matching array indexes. If you're working with a matrix of nested arrays, zip.apply can transpose the matrix in a similar fashion.

_.zip(['moe', 'larry', 'curly'], [30, 40, 50], [true, false, false]);
=> [["moe", 30, true], ["larry", 40, false], ["curly", 50, false]]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.