作为测试一些代码的副作用,我编写了一个小函数来比较使用array.push方法与直接寻址(array [n] = value)的速度.令我惊讶的是,推送方法通常表现得更快,特别是在Firefox中,有时在Chrome中.只是出于好奇:任何人都有解释吗?您可以在此页面找到测试(单击"数组方法比较")
可能重复:
在jquery中查找数组的长度(大小)
我有一个从php文件返回的json对象,如下所示: -
{"food":[
{
"name" :"Belgian Waffles",
"price" :"$5.95",
"description":"two of our famous Belgian Waffles with plenty of real maple syrup",
"calories" :"650"
},
{
"name" :"Strawberry Belgian Waffles",
"price" :"$7.95",
"description":"light Belgian waffles covered with strawberries and whipped cream",
"calories" :"900"
},
{
"name" :"Berry-Berry Belgian Waffles",
"price" :"$8.95",
"description":"light Belgian waffles covered with an assortment of fresh berries and whipped cream",
"calories" :"900"
}
]}
Run Code Online (Sandbox Code Playgroud)
这基本上是从物体中返回三个食物元素.如果json数据是动态的并且使用jquery不知道,如何计算返回的元素总数?
我有类似下面的javascript代码来获取数组的大小,
var testvar = [ ];
testvar[1] = 2;
testvar[200] = 3;
alert(testvar.length); //201Run Code Online (Sandbox Code Playgroud)
但代码警告201提到了大小2,为什么?
我需要得到该数组中的总元素数.