Remove entry from array using another array

前端 未结 7 1977
迷失自我
迷失自我 2021-01-25 09:12

Not sure how do to this, so any help is greatly appreciated

Say I have :

const array1 = [1, 1, 2, 3, 4];
const array2 = [1, 2];

Desired

7条回答
  •  夕颜
    夕颜 (楼主)
    2021-01-25 09:22

    var array1 = [1, 1, 2, 3, 4],
      array2 = [1, 2],
      result = array1.slice(0);
    
    array2.forEach(function(element) {
      var index = result.indexOf(element)
      if (index >= 0) {
        result.splice(index, 1)
      }
    })
    console.log(result)

提交回复
热议问题