简体   繁体   English

Javascript将值放在二维数组中

[英]Javascript putting values in two-dimensional array

Is there a better way to create a two-dimensional array in javascript than this? 有没有更好的方法在javascript中创建一个二维数组比这个?

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. 我真的不知道什么时候添加额外的坏数据,因为如果我在填充数组的循环期间发出警报,它似乎只循环3次以放置预期的三对值。

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("compage") != -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.com/iwuqab jsBin http://jsbin.com/iwuqab

The recommended method for creating arrays in JS is to NOT use the 'new' method. 在JS中创建数组的推荐方法是不使用'new'方法。 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() ). 您的二维数组已正确设置(好, []是首选而不是new Array() )。 The actual problem is only with the display of your array using alert(divcookies) . 实际问题仅在于使用alert(divcookies)显示数组。 Here, divcookies is converted to a string using the predefined method toString() . 这里, divcookies使用预定义的方法toString()转换为字符串。 This method creates a list of comma-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("compage") == -1 . 在你的情况,你是不是分配给这些索引idivcookies针对name_value[0].search("compage") == -1 These are the gaps ,, in the alerted list. 这些都是空白,,在提醒列表中。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM