猿问

在线等,挺急的!根据id值的不同做筛选?

就是拿一组ID跟另一个数组做比较如果有相同得放到相同得新数组里有不相同的放到不相同数组中
湖上湖
浏览 283回答 2
2回答

largeQ

[...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

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]
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答