javascript - Strange undefined jquery error -
i trying itterate trough jquery array, , having error script causing error is:
$.each(amount, function (key, value) { console.info('>>> selected line: '+value.value + " " + value.currency); if ((value.currency == currency) && (value.value == val)) { amount.splice(key,1); console.info('deleted: [' + value.value + " " + value.currency+ "] line "+ key); } });
the error firebug throws is:
typeerror: value undefined
could point me error or how fix error?
the issue .splice()
. when remove item 0, moves spot, no longer have item 1.
generally speaking, can't remove items list you're enumerating (unless taking steps adjust current index when adding or removing items, but.. yuck).
i recommend using filter function grep instead:
var newarr = $.grep(amount, function(item, idx) { return item.currency == currency || item.value == val; }, true);
Comments
Post a Comment