最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

jquery - Combaining two array into single multi dimensional array in javascript - Stack Overflow

programmeradmin4浏览0评论
status_name=Array("a","b","c","b","e","f");
status_id=Array( 1, 2, 3, 4, 5, 6);

How to bine 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

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

How to bine 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

Share Improve this question asked Mar 10, 2011 at 10:05 Mohan RamMohan Ram 8,46325 gold badges85 silver badges131 bronze badges
Add a ment  | 

6 Answers 6

Reset to default 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 ing 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 bined = [], length = Math.min(status_name.length, status_id.length);
for(var i = 0; i < length; i++) {
    bined.push([status_name[i], status_id[i]]);
}

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

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

try

function array_bine (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/functions/array_bine
    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld)
    // +   improved by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: array_bine([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 bine
- array bine

发布评论

评论列表(0)

  1. 暂无评论