Javascript algorithm to find elements in array that are not in another array

前端 未结 8 1101
情歌与酒
情歌与酒 2020-11-27 06:19

I\'m looking for a good algorithm to get all the elements in one array that are not elements in another array. So given these arrays:

var x = [\"a\",\"b\",\         


        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-11-27 07:04

    Late answer with the new ECMA5 javascript:

    var x = ["a","b","c","t"];
    var y = ["d","a","t","e","g"];
    
    myArray = y.filter( function( el ) {
      return x.indexOf( el ) < 0;
    });
    

提交回复
热议问题