简体   繁体   English

javascript得到子阵列长度

[英]javascript get subarray length

I was wondering if there's a way to get the length of a second-level array, for example : 我想知道是否有办法获得二级数组的长度,例如:

var arr = new Array();
arr[0] = new Array();

arr[0][0] = 'a';
arr[0][1] = 'a';
arr[0][2] = 'a';

I tried this, but without success : 我尝试过这个,但没有成功:

arr[0].length;

Cheers! 干杯!

EDIT 编辑

The evil code is as follows. 邪恶的代码如下。

This is the function that I use to fill the array, wich works as expected : 这是我用来填充数组的函数,它按预期工作:

function input_text(action, id) {
    if (action == 'add') {
        var i = info.length;
        if (i != 0) i++;
        info[i] = new Array();
        info[i]['type'] = 'input';
        info[i]['subtype'] = 'text';
        info[i]['nome'] = $('#input_text_form input[name="input_text_nome"]').val();
        info[i]['name'] = $('#input_text_form input[name="input_text_name"]').val();
        info[i]['id'] = $('#input_text_form input[name="input_text_id"]').val();
        info[i]['maxlenght'] = $('#input_text_form input[name="input_maxlenght"]').val();
        info[i]['default'] = $('#input_text_form input[name="input_text_default"]').val();
        info[i]['js'] = $('#input_text_form input[name="input_text_js"]').val();
    }
}

.. and this is a function to build a JSON string from the array. ..这是一个从数组中构建JSON字符串的函数。 You may notice that I count the sublevel arrays length several times, in order to prevent the string from ending wrong, like ,} 您可能会注意到我多次计算子级数组长度,以防止字符串结束错误,例如,}

function toJSON () {
    var fll = info.length;
    var sll = 0;
    var tll = 0;
    var i;
    var x;
    var z;
    var w;
    var b;
    json = '{';
    for (i in info) {
        json += '"'+i+'":{';
        sll = info[i].length;
        alert(sll);
        z = 0;
        for (x in info[i]) {
            if ($.isArray(info[i][x]))  {
                json += '"'+x+'":{';
                tll = info[i][x].length;
                w = 0;
                for (b in info[i][x]) {
                    tll == w ? json += '"'+b+'" : "'+info[i][x][b]+'"' : json += '"'+b+'" : "'+info[i][x][b]+'",';
                    w++;
                }
                sll == z ? json += '}' : json += '},';
            } else {
                sll == z ? json += '"'+x+'" : "'+info[i][x]+'"' : json += '"'+x+'" : "'+info[i][x]+'",';
            }
            z++;
        }
        fll == i ? json += '}' : json += '},';
    }

    json += '}';
}

Everytime I print the value of any of the fll, sll and tll variables, it gives me zero. 每次我打印任何fll,sll和tll变量的值时,它都给我零。

You are essentially creating an object with the string indexes. 您实际上是使用字符串索引创建对象。 You can only get the length if it is a true array. 如果它是真正的数组,则只能获得长度。

arr[0] = [];
arr[0][0] = 134;
arr[0][1] = 264;

arr[0].length; // will work

arr[1] = {};
arr[1]['str1'] = 134;
arr[1]['str2'] = 256;

arr[1].length; // will not work

See this question for more info: Length of a JavaScript object 有关详细信息,请参阅此问题: JavaScript对象的长度

Did you mispell it? 你错了吗? Try: 尝试:

arr[0].length;

It works for me: 这个对我有用:

var arr = new Array();
arr[0] = new Array();

arr[0][0] = 'a';
arr[0][1] = 'a';
arr[0][2] = 'a';

console.log(arr[0].length);

Result: 结果:

3

Check for yourself here. 在这里自己检查一下。

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

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