2
status_name=Array("a","b","c","b","e","f");
status_id=Array( 1, 2, 3, 4, 5, 6);

How to combine these two arrays and to built multi dimensional array Expected Multidimensional array be like this

[["a", 1],["b", 2],["c", 3],["d", 4],["e", 5],["f", 6]]

Help me how to use above two array values and built my expected multidimensional array

6 Answers 6

7

Since you're including jQuery, you can use jQuery.map in a similar fashion to Linus' answer:

var result      = [],
    status_name = ["a","b","c","b","e","f"],
    status_id   = [1, 2, 3, 4, 5, 6];

result = $.map(status_name, function (el, idx) {
    return [[el, status_id[idx]]];
}); 

Looking at your variable names, I'd guess that your coming from a language (like PHP). If that's the case, make sure you remember to declare local variables with the var keyword, otherwise you'll be polluting the global scope and you'll run into some hideous bugs in IE.

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

Comments

5

JavaScript has no buitin method for this, but you can easily write it yourself:

function zip(arrayA, arrayB) {
    var length = Math.min(arrayA.length, arrayB.length);
    var result = [];
    for (var n = 0; n < length; n++) {
        result.push([arrayA[n], arrayB[n]]);
    }
    return result;
}

The name zip is chosen because a function that does something like this is often called zip in other languages.

Comments

1

I tried Myself and brought this solution, It might help some one

  status_name=Array("a","b","c","b","e","f");
    status_id=Array( 1, 2, 3, 4, 5, 6);

Script:

            Values=[];
            for (i = 0; i < status_name.length; ++i)
            {
                Values[i] =Array(status_name[i], status_id[i]);
            }

Comments

1

Using jQuery.map

var status_name = ["a","b","c","b","e","f"],
    status_id = [1,2,3,4,5,6],
    r = [];

r = $.map(status_name, function(n, i) {
    return [[n, status_id[i]]]; 
});

Note the difference between return [[n, status_id[i]]] and return [n, status_id[i]]. Using the former will result in a 2d array while using the latter will result in a 1d array.

Comments

1
var combined = [], length = Math.min(status_name.length, status_id.length);
for(var i = 0; i < length; i++) {
    combined.push([status_name[i], status_id[i]]);
}

You could also use Array.prototype.map, but that's not supported in all browsers:

var combined = status_name.map(function(name, index) { return [name, status_id[index]] });

1 Comment

you meant that ? combined.push([status_name[i], status_id[i]); a square bracket excess dude
0

try

function array_combine (keys, values) {
    // Creates an array by using the elements of the first parameter as keys and the elements of the second as the corresponding values  
    // 
    // version: 1102.614
    // discuss at: http://phpjs.org/functions/array_combine
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: array_combine([0,1,2], ['kevin','van','zonneveld']);
    // *     returns 1: {0: 'kevin', 1: 'van', 2: 'zonneveld'}
    var new_array = {},
        keycount = keys && keys.length,
        i = 0;

    // input sanitation
    if (typeof keys !== 'object' || typeof values !== 'object' || // Only accept arrays or array-like objects
    typeof keycount !== 'number' || typeof values.length !== 'number' || !keycount) { // Require arrays to have a count
        return false;
    }

    // number of elements does not match
    if (keycount != values.length) {
        return false;
    }

    for (i = 0; i < keycount; i++) {
        new_array[keys[i]] = values[i];
    }

    return new_array;

Reference
- arr combine
- array combine

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.