Javascript 将值放入二维数组中

发布于 2024-11-28 07:23:36 字数 1305 浏览 1 评论 0原文

在javascript中还有比这更好的创建二维数组的方法吗?

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

这会导致二维数组的中间添加了错误数据。我期待这样的数组。

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

相反,我得到了这个。

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

我真的不知道何时添加额外的坏数据,因为如果我在填充数组的循环期间发出警报,它似乎只会循环 3 次以放入预期的三对值。

所以我正在寻找一种不同的方法来获取二维数组。

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

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("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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绳情 2024-12-05 07:23:37

您的二维数组已正确设置(最好是 [] 而不是 new Array())。实际问题仅在于使用 alert(divcookies) 显示数组。此处,使用预定义方法 toString()divcookies 转换为字符串。此方法创建一个以逗号分隔的数组元素列表,从第一个元素到最后一个元素。如果未设置中间的某些元素,则输出空字符串。在您的情况下,您没有分配给 divcookies 的那些索引 i ,其中 name_value[0].search("compage") == -1.这些是警报列表中的间隙 ,,

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 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. These are the gaps ,, in the alerted list.

罪歌 2024-12-05 07:23:36

在 JS 中创建数组的推荐方法是不要使用“new”方法。相反,这样做:

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

此表示法使您不必提前指定元素编号。一旦变量被初始化为数组,您就可以设置所需的任何索引。缺点(无论您使用哪种表示法)是您还必须初始化每个子数组。

The recommended 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文