就是拿一组ID跟另一个数组做比较如果有相同得放到相同得新数组里有不相同的放到不相同数组中
2 回答
UYOU
TA贡献1878条经验 获得超4个赞
[...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:[]})
SMILET
TA贡献1796条经验 获得超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
提交
取消