在 jquery 中查找数组的长度(大小)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5317298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 18:57:20  来源:igfitidea点击:

Find length (size) of an array in jquery

jqueryarrays

提问by Ed.

I think I'm going crazy. I have a simply question that I'm struggling with for some reason.

我想我快疯了。我有一个简单的问题,由于某种原因我正在努力解决。

Why does the below return 'undefined'?

为什么下面返回“未定义”?

var testvar={};
testvar[1]=2;
testvar[2]=3;
alert(testvar.length);

editI originally typed testvar[1].length. I knew this to be an error. I meant testvar.length

编辑我最初输入 testvar[1].length。我知道这是一个错误。我的意思是 testvar.length

回答by Cameron

Because 2isn't an array, it's a number. Numbers have no length.

因为2不是数组,而是数字。数字没有长度。

Perhaps you meant to write testvar.length; this is also undefined, since objects (created using the { ... }notation) do not have a length.

也许你打算写testvar.length;这也是未定义的,因为对象(使用{ ... }符号创建)没有长度。

Only arrays have a length property:

只有数组有长度属性:

var testvar = [  ];
testvar[1] = 2;
testvar[2] = 3;
alert(testvar.length);    // 3

Note that Javascript arrays are indexed starting at 0and are not necessarily sparse(hence why the result is 3 and not 2 -- see this answerfor an explanation of when the array will be sparse and when it won't).

请注意,Javascript 数组从 开始索引0并且不一定是稀疏的(因此为什么结果是 3 而不是 2 - 请参阅此答案以了解数组何时稀疏以及何时不会稀疏)。

回答by mVChr

testvar[1] is the value of that array index, which is the number 2. Numbers don't have a length property, and you're checking for 2.length which is undefined. If you want the length of the array just check testvar.length

testvar[1] 是该数组索引的值,即数字 2。数字没有长度属性,您正在检查未定义的 2.length。如果你想要数组的长度,只需检查 testvar.length

回答by Dim_K

Integer has no method length. Try string

整数没有方法长度。尝试字符串

var testvar={};
testvar[1]="2";
alert(testvar[1].length);

回答by RafaSashi

If lengthis undefined you can use:

如果length未定义,您可以使用:

            function count(array){

                var c = 0;
                for(i in array) // in returns key, not object
                    if(array[i] != undefined)
                        c++;

                return c;
            }

            var total = count(array);

回答by kush

       var mode = [];
                $("input[name='mode[]']:checked").each(function(i) {
                    mode.push($(this).val());
                })
 if(mode.length == 0)
                {
                   alert('Please select mode!')
                };

回答by Tanmaya

var array=[];

array.push(array);  //insert the array value using push methods.

for (var i = 0; i < array.length; i++) {    
    nameList += "" + array[i] + "";          //display the array value.                                                             
}

$("id/class").html(array.length);   //find the array length.

回答by Tanmaya

          obj={};

        $.each(obj, function (key, value) {
            console.log(key+ ' : ' + value); //push the object value
        });

        for (var i in obj) {
         nameList += "" + obj[i] + "";//display the object value
        }
           $("id/class").html($(nameList).length);//display the length of 
                                                       object.