我正在使用Rails,我的jQuery代码出错:
Uncaught TypeError: Cannot read property 'length' of undefined
我收到错误的实际代码:
// args is for internal usage only
each: function( obj, callback, args ) {
var value,
i = 0,
length = obj.length,
isArray = isArraylike( obj );
if ( args ) {
if ( isArray ) {
for ( ; i < length; i++ ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break;
}
}
} else {
for ( i in obj ) {
value = callback.apply( obj[ i ], args );
if ( value === false ) {
break;
}
}
}
答案 0 :(得分:3)
试试这个怎么样?
var length = Object.keys(obj).length
或强>
var length = 0;
for (var o in obj) {
length++;
}