3

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

6
  • thanks, still, I've tryed with "both words" without success. Commented Aug 2, 2010 at 18:15
  • 1
    * Looks at title * Well, at least he's consistent. More than I can say for many programmers. Commented Aug 2, 2010 at 18:16
  • 1
    You should instantiate your arrays using [] instead of new Array(). Mostly the same, but [] is much shorter and the Array function can be messed with. Commented Aug 2, 2010 at 18:26
  • Thanks @Daniel, but in this case apparently changed nothing, so I suppose it wasn't messing arround. Still, I'll start using that method :) Commented Aug 2, 2010 at 18:29
  • 1
    Just wondering: why don't you use JSON.stringify from json2.js? Commented Aug 2, 2010 at 19:18

3 Answers 3

5

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

Sign up to request clarification or add additional context in comments.

1 Comment

So there's no option here .. guess I'll have to explode the last character of the JSON string "by hand"
2

Did you mispell it? Try:

arr[0].length;

2 Comments

@yoda - Yes it does. Check again.
What are you expecting it to produce, and what is it producing?
2

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.

3 Comments

then could it as something to do with the array keys being strings?
@Sarfraz's code worked in Chrome for me exactly as I would expect. I'm not sure why you are having this problem, yoda, but I think that you'll need to provide more information about your environment as well as a full code sample.
The array keys are not strings here, they're numbers. The values are strings.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.