Filter and delete filtered elements in an array

前端 未结 6 1482
遥遥无期
遥遥无期 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条回答
  •  不思量自难忘°
    2020-12-09 17:18

    So if I understood, you want to remove the elements that matches the filter from the original array (a) but keep them in the new array (b) See if this solution is what you need:

    var a = [{name:'tc_001'}, {name:'tc_002'}, {name:'tc_003'}]
    
    var b = a.filter(function (e) {
          return e.name === 'tc_002'
    });
    
    b.forEach(function(element) {
       console.log(element)
       var index = a.indexOf(element)
       console.log(index)
       a.splice(index, 1)
    })
    

    Result: a = [{"name":"tc_001"},{"name":"tc_003"}] b = [{"name":"tc_002"}]

提交回复
热议问题