开发者

Combaining two array into single multi dimensional array in javascript

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 mul开发者_如何学Gotidimensional array


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.


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.


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]);
            }


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.


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]] });


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜