let refList = ['a', 'b', 'c', 'd', 'e']; let choose = ['b', 'd', 'e']; let res = refList.filter( el => { return choose.indexOf(el)>-1 } ) console.log('res', res)
res (3) ["b", "d", "e"]
更为复杂的运用
let r1=['a','e']; let c1=[ {studio:"CDU", group:[ { name:'b' }, { name:'d' }, { name:'a' }, , { name:'c' }, , { name:'e' }, ] } ] //c1为总数组,r1为用于过滤的数组 let res1=c1.map( el=>{ let aa = el.group.filter( t=>{ return r1.indexOf(t.name)>-1; } ) console.log('AA',aa) return aa } ) console.log('res1',res1)
res1