就是拿一组ID跟另一个数组做比较如果有相同得放到相同得新数组里有不相同的放到不相同数组中
2 回答
largeQ
TA贡献2039条经验 获得超7个赞
[...arr1,...arr2].reduce((result,v)=>{letindex=result.diff.indexOf(v)if(index!==-1){result.same.push(result.diff.splice(index,1)[0]);}else{result.diff.push(v);}returnresult;},{same:[],diff:[]})
MYYA
TA贡献1868条经验 获得超4个赞
consta=[1,2,3,4];constb=[2,3,5,6];functionfilterArr(arr1,arr2,type){letdiffArr=[];letsameArr=[];sameArr=arr1.filter(v=>{returnarr2.some(a=>{returnv==a})})letmergeArr=[...arr1,...arr2];diffArr=mergeArr.filter(v=>{return!sameArr.includes(v)})if(type=='same'){returnsameArr}else{returndiffArr}}console.log(filterArr(a,b,'same'))//[2,3]console.log(filterArr(a,b,'diff'))//[1,4,5,6]
添加回答
举报
0/150
提交
取消