jquery stop if result is null or undefined -


i have script looks this:

function bakvormshipping(targetclass) {     $.getjson('http://shop.com/cart/?format=json', function (data) {         $.each(data.cart.shipping.methods, function (index, methods) {             if (index == "core|2419|36557" || index == "core|2419|36558" || index == "core|2959|31490" || index == "core|2959|31491" || index == "core|2419|36556") {                 $('<strong/>').html('€' + (+methods.price.price_incl).tofixed(2)).appendto(targetclass);             }         });     }); } 

i undefined or null error in firebug when "index" doesn't equals 1 of indexes. how can prevent that? if (index == null) etc. don't have clue on how in right way.

add check undefined above if statement

function bakvormshipping(targetclass) { $.getjson('http://shop.com/cart/?format=json', function (data) {      $.each(data.cart.shipping.methods, function (index, methods) {         if(typeof(index) == "undefined"){             return;         }          if (index == "core|2419|36557" || index == "core|2419|36558" || index == "core|2959|31490" || index == "core|2959|31491" || index == "core|2419|36556") {             $('<strong/>').html('€' + (+methods.price.price_incl).tofixed(2)).appendto(targetclass);         }      }); }); 

you might want check data object recieve has properties referencing, data.cart, data.cart.shipping , data.cart.shipping.methods


Comments