记录 jquery 循环删除多个数组元素
var array = [ { id: 1, name: 'test1'}, { id: 2, name: 'test2'}, { id: 3, name: 'test3'}, { id: 4, name: 'test1'} ]; //splice()清除name为test1的元素 $.each(array, function(index, value) { if(value.name === 'test1') { array.splice(index, 1) } });
复制
删除第一个test1,所有元素的下标往前移了,当遍历到第二个test1时找不到,所以报undefined
array.filter(res => res.name === 'test1').forEach( res => array.splice(array.indexOf(res), 1) );
复制
可以先过滤再遍历取下标