奇怪的未定义jquery错误

Strange undefined jquery error

本文关键字:jquery 错误 未定义      更新时间:2023-09-26

我试图通过jquery数组迭代,我有一个错误导致错误的脚本是:

$.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+ "] from line "+ key);
    }
});

firebug抛出的错误是:

TypeError: value is undefined

有人能告诉我错误在哪里或如何修复错误吗?

问题是你的.splice()。当你移除物品0时,所有物品都向上移动一个位置,所以你不再拥有物品1。

一般来说,您不能从枚举的列表中删除项(除非在添加或删除项时采取措施调整当前索引),但是..恶心)。

我建议使用像grep这样的过滤函数:

http://jsfiddle.net/DnN4a/

var newArr = $.grep(amount, function(item, idx) {
   return item.currency == currency || item.value == val; 
}, true);