Comparing two arrays in jquery

前端 未结 5 2167
臣服心动
臣服心动 2020-12-20 02:46

Using this code...

var a = [\'volvo\',\'random data\'];
var b = [\'random data\'];
var unique = $.grep(a, function(element) {
    return $.inArray(element, b         


        
5条回答
  •  陌清茗
    陌清茗 (楼主)
    2020-12-20 03:26

    You could just iterate over a and use Array.prototype.indexOf to get the index of the element in b, if indexOf returns -1 b does not contain the element of a.

    var a = [...], b = [...]
    a.forEach(function(el) {
        if(b.indexOf(el) > 0) console.log(b.indexOf(el));
        else console.log("b does not contain " + el);
    });
    

提交回复
热议问题