Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.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
Javascript 如何在CanJS中获得延迟模型的长度?_Javascript_Canjs_Canjs Model - Fatal编程技术网

Javascript 如何在CanJS中获得延迟模型的长度?

Javascript 如何在CanJS中获得延迟模型的长度?,javascript,canjs,canjs-model,Javascript,Canjs,Canjs Model,我得到一个模型延迟对象,其结构如下: can.each(myObject,function(myObject,index){ // Get the last index value and put it into some variable }); 我如何找出这个对象中有多少个对象(在本例中为三个)。如果我使用Object.keys(myObject).length,它包括对象观察者和其他数据,如_computeBindings,_bindings等。我甚至在计算长度时尝试使用hasOwnP

我得到一个模型延迟对象,其结构如下:

can.each(myObject,function(myObject,index){
// Get the last index value and put it into some variable
});

我如何找出这个对象中有多少个对象(在本例中为三个)。如果我使用
Object.keys(myObject).length
,它包括对象观察者和其他数据,如_computeBindings,_bindings等。我甚至在计算长度时尝试使用
hasOwnProperty
,但它没有给我想要的结果

我可以找到的一种方法是迭代索引并获得最后一个索引值,如下所示:

can.each(myObject,function(myObject,index){
// Get the last index value and put it into some variable
});
有用于此的API吗?

can.Map有一个函数,该函数将在地图中为您提供一个键数组,您可以通过检查该数组的长度从中获取多少个对象


使用can.List作为数据结构也可以。数据中的键是数字的,您需要检查长度以及所有可以检查的内容。列表是为其生成的。

尝试使用
myObject.attr('length')
模型数据属性只包含一个没有绑定的模型副本。最简单的方法是使用:

Object._data.length    
can.Map.keys(myObject).length将解决此问题:)非常感谢:)