Remove entry from array using another array

前端 未结 7 1971
迷失自我
迷失自我 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:48

    You could take a Map for counting the items for removing.

    const
        array1 = [1, 1, 2, 3, 4],
        array2 = [1, 2],
        remove = array2.reduce((m, v) => m.set(v, (m.get(v) || 0) + 1), new Map),
        result = array1.filter(v => !remove.get(v) || !remove.set(v, remove.get() - 1));
    
    console.log(...result);

    0 讨论(0)
提交回复
热议问题