当前位置: 首页 > Web前端 > JavaScript

js根据某个值或数组过滤出object数组中符合条件的集合

时间:2023-03-27 00:29:17 JavaScript

js根据某个值或数组过滤出object数组中符合条件的集合,使用filter和includes:Case1:consttarget=[{a:[1,2,3]},{a:[11,22,31]},{a:[9,7,3]}];consttempSpecId=3;constselectedArray=target.filter((item)=>{returnitem.a.includes(tempSpecId)});console.log(selectedArray,'selectedArray-')//[{a:[1,2,3]},{a:[9,7,3]}]情况2:constspecIds=[1,2,3],4];constproductList=[{a:1},{a:22},{a:3}];constselectedArray=productList.filter(item=>{return!specIds.includes(item.a)});console.log(selectedArray,'selectedArray')//[{a:22}]