admin管理员组

文章数量:1336699

Is there a better way to create a two-dimensional array in javascript than this?

var divcookies = new Array();
divcookies[0] = new Array(2);
divcookies[0][0] = name;
divcookies[0][1] = value;

This results in a two dimensional array with bad data added in the middle of it. I expect an array like this.

(name1, value1, name2, value2, name3, value3)

Instead I get this.

(name1, value2, ,name2, value2, name3, value3)

I don't really know when that extra bad data is added because if I alert during the loop that fills the array it only seems to loop through 3 times to put the three pairs of values expected.

So I am looking for a different way to get the two dimensional array.

function get_cookies_array() {

    var divcookies = new Array();

    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            if ( name_value[0].search("page") != -1 ) {
               alert (divname+" "+ divarray);
               divcookies[i] = new Array(2);
               divcookies[i][0] = decodeURIComponent(name_value[0]);
               divcookies[i][1] = decodeURIComponent(name_value[1]);
            }
        }
    }

    alert (divcookies);

    return divcookies;

}

jsBin

Is there a better way to create a two-dimensional array in javascript than this?

var divcookies = new Array();
divcookies[0] = new Array(2);
divcookies[0][0] = name;
divcookies[0][1] = value;

This results in a two dimensional array with bad data added in the middle of it. I expect an array like this.

(name1, value1, name2, value2, name3, value3)

Instead I get this.

(name1, value2, ,name2, value2, name3, value3)

I don't really know when that extra bad data is added because if I alert during the loop that fills the array it only seems to loop through 3 times to put the three pairs of values expected.

So I am looking for a different way to get the two dimensional array.

function get_cookies_array() {

    var divcookies = new Array();

    if (document.cookie && document.cookie != '') {
        var split = document.cookie.split(';');
        for (var i = 0; i < split.length; i++) {
            var name_value = split[i].split("=");
            name_value[0] = name_value[0].replace(/^ /, '');
            if ( name_value[0].search("page") != -1 ) {
               alert (divname+" "+ divarray);
               divcookies[i] = new Array(2);
               divcookies[i][0] = decodeURIComponent(name_value[0]);
               divcookies[i][1] = decodeURIComponent(name_value[1]);
            }
        }
    }

    alert (divcookies);

    return divcookies;

}

jsBin http://jsbin./iwuqab

Share Improve this question asked Aug 5, 2011 at 19:25 user823527user823527 3,71217 gold badges69 silver badges111 bronze badges 1
  • No it is not the best way. However what are you trying to do? Here is a cookie script quirksmode/js/cookies.html – mplungjan Commented Aug 5, 2011 at 19:32
Add a ment  | 

2 Answers 2

Reset to default 3

The remended method for creating arrays in JS is to NOT use the 'new' method. Instead, do:

var divcookies = [];
divcookies[0] = [];
divcookies[0][0] = name;
divcookies[0][1] = value;

This notation frees you up from having to specify element numbers in advance. Once a variable's been initialized as an array, you can set any index you want. The downside (regardless of which notation you use) is that you have to initialize every sub-array as well.

Your 2-dimensional array is set up correctly (well, [] is preferred instead of new Array()). The actual problem is only with the display of your array using alert(divcookies). Here, divcookies is converted to a string using the predefined method toString(). This method creates a list of ma-separated array elements, from the first element to the last element. If some elements in between are not set, an empty string is output. In your case, you are not assigning to those indexes i of divcookies for which name_value[0].search("page") == -1. These are the gaps ,, in the alerted list.

本文标签: Javascript putting values in twodimensional arrayStack Overflow