Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/javascript/432.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用jQuery的javascript关联数组长度_Javascript_Jquery - Fatal编程技术网

使用jQuery的javascript关联数组长度

使用jQuery的javascript关联数组长度,javascript,jquery,Javascript,Jquery,我使用javascript关联数组,如: var testarray = []; testarray['one'] = '1'; testarray['two'] = '2'; testarray['three'] = '3'; 我还同时使用jquery。如何使用jquery或任何其他方法检查此关联数组的长度?基本上我想检查这个数组是否为空 谢谢。没有直接的“长度”或“大小”调用,您必须测试对象中可用的键 请注意,所有JavaScript对象都是关联数组(映射),因此您的代码最好使用通用对象而

我使用javascript关联数组,如:

var testarray = [];
testarray['one'] = '1';
testarray['two'] = '2';
testarray['three'] = '3';
我还同时使用jquery。如何使用jquery或任何其他方法检查此关联数组的长度?基本上我想检查这个数组是否为空

谢谢。

没有直接的“长度”或“大小”调用,您必须测试对象中可用的键

请注意,所有JavaScript对象都是关联数组(映射),因此您的代码最好使用通用对象而不是数组:

var testarray = {}; // <= only change is here
testarray['one'] = '1';
testarray['two'] = '2';
testarray['three'] = '3';
…使用它可以构建一个函数来测试对象是否为空

function isEmpty(obj) {
    var name;
    for (name in obj) {
        return false;
    }
    return true;
}
正如CMS在他的回答中指出的那样,这将遍历所有键,包括对象原型上的键。如果只需要对象上的关键点,而不需要其原型,请使用内置的
hasOwnProperty
功能:

function isEmpty(obj) {
    var name;
    for (name in obj) {
        if (obj.hasOwnProperty(name)) {
            return false;
        }
    }
    return true;
}

不应使用数组存储非数值索引,应使用简单对象:

function getObjectLength (o) {
  var length = 0;

  for (var i in o) {
    if (Object.prototype.hasOwnProperty.call(o, i)){
      length++;
    }
  }
  return length;
}
编辑:由于您使用的是jQuery,并且希望检查对象是否为“空”,因此1.4版本引入了


您实际上没有数组,因此我避免将其初始化为:

var testNotArray = { };
testNotArray['one'] = 'something';
// ...
这本身就很危险,但第一步可能是:

function objectSize(o) {
  var c = 0;
  for (var k in o) 
    if (o.hasOwnProperty(k)) ++c;
  return c;
}

同样,这种方法可能会以无数种奇怪的方式失败。

您可以按如下方式计算长度:

var testarray = {}; // Use a generic object to store non-numeric indexes not an array
testarray['one'] = '1';
testarray['two'] = '2';
testarray['three'] = '3';
var count = 0
for each(key in testarray)
 count = count + 1
alert(count); // count contains the number of items in the array

您可以循环查看属性以对其进行计数:

var cnt = 0;
for (i in testarray) cnt++;
alert(cnt);
请注意,(…in…)的
也将循环由原型添加的项目,因此您可能希望只计算之后添加的项目:

var cnt = 0;
for (i in testarray) if (testarray.hasOwnProperty(i)) cnt++;
alert(cnt);
如果只想检查是否存在任何属性,可以在第一项之后退出循环:

var empty = true;
for (i in testarray) if (testarray.hasOwnProperty(i)) { empty = false; break; }
alert(empty);

我也有这个问题,我刚刚意识到解决方案(并在我的浏览器中进行了测试):

让我知道这是否适合你!我将它直接写入我的代码中,如下所示:

var devices = STATUS.devices,
  num_devices = 0;
$.each(devices, function(id, device) { num_devices+=1; } );
//num_devices set to number of devices

这将为您提供关联数组的长度:

Object.keys(testarray).length

欺骗好答案@JavaScript没有“关联数组”,它有“数组”和“对象”。如果要使用命名属性,请使用普通对象(
{}
)而不是数组。@David:语义点,但我可以用另一种方式说:所有JavaScript对象都是关联数组(不仅仅是
数组
类型,当然也是)。它们具有可枚举的字符串键、可临时添加和删除的条目(
delete
)等。。(同意如果你不使用数字索引,你就不想使用
数组
)另外,我想指出jquery只是一个库——没有必要担心“使用jquery”的问题,因为你可以使用普通的老javascript,即使您正处于由一些jQuery调用触发的函数回调的中间。Jquery不是一种语言,但它非常强大。@belugabob:+1而且它说的还不够多,尽管在这种特殊情况下,Jquery有帮助(参见CMS的答案)。噢,我喜欢从对象原型中获得“hasOwnProperty:-”),你不应该使用数组只存储非数字索引(正如我在回答中所说的),但我认为在用于“正常”目的的数组上存储非数字信息没有任何问题。为什么不在
o
本身上使用
hasOwnProperty
?为了避免它可能被替换?@T.J.Crowder:是的,安全第一,否则
getObjectLength({hasOwnProperty:'foo'})
将使函数抛出
TypeError
…谢谢,我想。不过,我想我还是听从对象作者的意见。如果他们有很好的理由重写它,我应该尊重它(即使这意味着他们自己把事情搞砸了)。而且不要忘记
hasOwnProperty()
不从原型中计算属性。可能应该使用“hasOwnProperty”技巧,如果我没有沉迷于stackoverflow,我就不会知道这一点:-)
//given: ass_arr, an associative array
//result: count now holds the "length" of ass_arr 
var count = 0;
$.each(ass_arr, function(key, val) { count+=1; } );
alert(count); //number of iterable attributes in ass_arr
var devices = STATUS.devices,
  num_devices = 0;
$.each(devices, function(id, device) { num_devices+=1; } );
//num_devices set to number of devices
Object.keys(testarray).length