remove common elements of two arrays in jquery

前端 未结 4 1470
粉色の甜心
粉色の甜心 2021-01-18 03:54

I want to remove common elements of two arrays in jquery. I have two arrays:

A = [0,1,2,3]
B = [2,3]

and result should be [0, 1]

4条回答
  •  生来不讨喜
    2021-01-18 04:36

    You can filter array A by checking its elements position in array B:

    C = A.filter(function(val) {
     return B.indexOf(val) == -1;
    });
    

    Demo

提交回复
热议问题