数组中去除某一部分数组

发布于 12 天前  29 次阅读 本文共466个字


道理很简单,就是直接利用数组filter方法将找(some)出来的数组过滤掉即可。

话不多说,举栗子:

// 例1
let ary = ['q', 'w', 'e', 'r', 't', 'y'];
const arr = ['q', 'y'];
ary = ary.filter(item => !arr.some(arrItem => arrItem === item));
console.log(ary)  //["w", "e", "r", "t"]
// 例2
let a = [{name:'gaojiarui',age:'18',id:'1231'},{name:'gjr',age:'16',id:'1324'},{namg:'gqjr',age:'17',id:'1234'}];
let b = [{name:'gjr',id:'1324'},{name:'gqjr',id:'1234'}]

a = a.filter(item => !b.some(aa => aa.id === item.id))
console.log(a);  //[{name: "gaojiarui", age: "18", id: "1231"}

努力,只为遇见更好的自己!