Comparing two arrays in jquery

前端 未结 5 1043
无人共我
无人共我 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:35

    Regarding your comment, here is a solution:

    with jQuery:

    $.each( a, function( key, value ) {
        var index = $.inArray( value, b );
        if( index != -1 ) {
            console.log( index );
        }
    });
    

    without jQuery:

    a.forEach( function( value ) {
        if( b.indexOf( value ) != -1 ) {
           console.log( b.indexOf( value ) );
        }
    });
    

提交回复
热议问题