leta=[['1010','20','10'],['3010','40','20']]letb=[{KUNN2:"0000000001",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'},{KUNN2:"0000000002",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'},{KUNN2:"0000000003",PARVW_TEXT:"收款方",VKORG:'4100',VTWEG:'30',VSBED:'10'},{KUNN2:"0000000004",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'},{KUNN2:"0000000005",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'}]如上面2面2个数组,数组a是匹配项,随后我想要的结果是letc=[[{KUNN2:"0000000002",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'},{KUNN2:"0000000005",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'}],[{KUNN2:"0000000001",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'},{KUNN2:"0000000004",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'}]]希望各位大佬指教,不甚感激。###题目描述题目来源及自己的思路相关代码//请把代码文本粘贴到下方(请勿用图片代替代码)你期待的结果是什么?实际看到的错误信息又是什么?
2 回答
慕尼黑8549860
TA贡献1818条经验 获得超11个赞
应该是你想要的吧leta=[['1010','20','10'],['3010','40','20']]letb=[{KUNN2:"0000000001",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'},{KUNN2:"0000000002",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'},{KUNN2:"0000000003",PARVW_TEXT:"收款方",VKORG:'4100',VTWEG:'30',VSBED:'10'},{KUNN2:"0000000004",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'},{KUNN2:"0000000005",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'}]constresult=[];for(constaItemofa){constbFilter=b.filter(({VKORG,VTWEG,VSBED})=>VKORG===aItem[0]&&VTWEG===aItem[1]&&VSBED===aItem[2]);bFilter.length>0&&result.push(bFilter);}console.log(result);
qq_笑_17
TA贡献1818条经验 获得超7个赞
1.你给的a数组,第一个值应该是1010对吧,2.a数组的值,我能过目测是分别与VKORG,VTWEG,VBED三个值相对应的,有没有不对应的需求,如果是对应的,哪就按下面代码来就可以leta=[['1010','20','10'],['3010','40','20']]letb=[{KUNN2:"0000000001",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'},{KUNN2:"0000000002",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'},{KUNN2:"0000000003",PARVW_TEXT:"收款方",VKORG:'4100',VTWEG:'30',VSBED:'10'},{KUNN2:"0000000004",PARVW_TEXT:"收款方",VKORG:'3010',VTWEG:'40',VSBED:'20'},{KUNN2:"0000000005",PARVW_TEXT:"收款方",VKORG:'1010',VTWEG:'20',VSBED:'10'}]letc=b.filter((item)=>{letindex=a.find((arr)=>{returnarr[0]===item.VKORG&&arr[1]===item.VTWEG&&arr[2]===item.VSBED})if(index){returnitem}})console.log(c)第二种方法,判断只要obj里的值含有数组里的值就返回,从你的回复里看到,a数组的子数组个数也不确定,但是问题如果存在以下数据,也会被返回{KUNN2:"0000000001",PARVW_TEXT:"收款方",VKORG:'10',VTWEG:'1010',VSBED:'20'}用下面的方法,这样的数据也是匹配的。letd=a.map((keys)=>{letres=b.filter((item)=>{letobjarr=Object.values(item)letoldlength=objarr.length//以下代码用于判断数组是否为另一个数组的子集,采用的方法是把两个数组合并,再去重后的数组长度等于原数组长度,则判定子集匹配。letnewarr=[...objarr,...keys]letnewlength=[...newSet(newarr)].lengthif(oldlength===newlength){returnitem}})returnres})
添加回答
举报
0/150
提交
取消
