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

前端 未结 8 1079
情歌与酒
情歌与酒 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条回答
  •  时光取名叫无心
    2020-11-27 06:57

    in ES6 simply

    const x = ["a", "b", "c", "t"];
    const y = ["d", "a", "t", "e", "g"];
    
    console.log( y.filter(e => !x.includes(e)) );

    (another option is y.filter(e => x.indexOf(e)===-1) )

提交回复
热议问题