Filter and delete filtered elements in an array

前端 未结 6 1495
遥遥无期
遥遥无期 2020-12-09 16:32

I want to remove specific elements in the original array (which is var a). I filter() that array and splice() returned new array. but

6条回答
  •  猫巷女王i
    2020-12-09 17:32

    I know I'm a little bit late to the party, but how about a little bit different approach?

    var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_002'}, {name:'tc_002'}, {name:'tc_003'}];
    
    while ( a.findIndex(e => e.name === 'tc_002' ) >= 0 )
     a.splice( a.findIndex(f => f.name === 'tc_002'),1);
     
    console.log(a);

    I know it has some drawbacks, but i hope, it will help some of you in some cases :)

    cheers!

提交回复
热议问题