remove common elements of two arrays in jquery

前端 未结 4 1428
粉色の甜心
粉色の甜心 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:41

    Checkout the library underscore.js.

    Say you have two arrays,

    var a = [0,1,2,3];
    var b = [2, 3];
    

    First find the union.

    var all = _.union(a, b);
    

    Then find the intersection.

    var common = _.intersection(a, b);
    

    The final answer should be the difference between the union, and the intersection.

    var answer = _.difference(all, common)
    

提交回复
热议问题