Getting TypeError when iterating over an object

352
November 22, 2016, at 10:02 AM

I am using Rails, and I am getting an error in my jQuery code:

Uncaught TypeError: Cannot read property 'length' of undefined

The actual code where I am getting the error:

// 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;
                    }
                }
            }
Answer 1

how about try this?

var length = Object.keys(obj).length

or

var length = 0;
for (var o in obj) {
    length++;
}
Rent Charter Buses Company
READ ALSO
Having multiple countdown timer in javascript with dynamic arguments

Having multiple countdown timer in javascript with dynamic arguments

I have implemented multiple countdown timer by using this jsfiddle link.

502
Passing Array to WebMethod using jquery ajax is not working in asp.net

Passing Array to WebMethod using jquery ajax is not working in asp.net

I am trying to send all gridview records to webmethod using jquery ajax but it is not working. Here is my code.

500
Semantic UI adding class dynamically to input field

Semantic UI adding class dynamically to input field

It seems there is an issue with adding class dynamically to input field in SUI. .

816
Show and hide parent and children divs using jQuery

Show and hide parent and children divs using jQuery

I have a div which contains children html elements, when I click on div children elements will display, in that hide button also there when I click on hide parent will hide. [nnn]please help me.

1122