开发者

Checking for Null in an Object

开发者 https://www.devze.com 2023-03-26 10:49 出处:网络
for ( var i=0; i<MyArrayObj.length; 开发者_StackOverflowi++ ) { } I am getting an error called, Cannot call MyArrayObj, since its is NULLAssuming there isn\'t a logical error with MyArrayObj bei
for ( var i=0; i<MyArrayObj.length; 开发者_StackOverflowi++ ) {

}

I am getting an error called, Cannot call MyArrayObj, since its is NULL


Assuming there isn't a logical error with MyArrayObj being null:

if(MyArrayObj) {
    for ( var i=0; i<MyArrayObj.length; i++ ) {

    }
}


You can also add this line above your loop just to be sure and avoid having to check at all:

MyArrayObj = MyArrayObj || [];

This will assign empty array into the variable in case it's null or undefined.


Did you declare the MyArrayObj and assigned a correct value to it?

Could you provide the code where you assign the array to a value?

Otherwise check if the array is not null first.


So that means you must not have initialized myArrayObj yet. You have to put something in the array before you can iterate through it.

0

精彩评论

暂无评论...
验证码 换一张
取 消