js i++循环数组时,splice 删除数据没有i-- ,得到的数据错误:
let rId=2; let list=[{id: 1,name:'a'}、{id:name3360'd'}、{id:name3360'c'}、{id:'c'} i list.length; I () if ) list[I].idrid ) ) list.splice(I,1 ); } console.log (列表;
正确使用:
去除//id大于2的数据for (leti=0; i list.length; I () if ) list[I].idrid ) ) list.splice(I,1 ); I----; //数组本身变了,长度变了,数组元素进了一步,循环也进了一步}}console.log(list ); //或直接循环时I--for(letI=list.length-1; i=0; I--}{if(list[I].idrid ) list.splice(I,1 ); } console.log (列表;