for循环嵌套

我有两个数组 A,B,然后我拿到A的面的每一个值去跟B里面的每一个值进行对比,有的话就取出A放在数组里,如果没有就在数组里补个空,这个循环要怎么写,本来A的长度都是12,结果返回了一个A*B长度的数组,但是我想返回的还是长度是12的数组,只不过A里有的,B没有,就在对应的位置补空

浮云间
浏览 520回答 1
1回答

慕盖茨4494581

A.map(v=>B.indexOf(v)<0?"":v)var A = ["a","b","c"];var B = ["b"];function func(A,B){&nbsp; &nbsp; var arr = [];&nbsp; &nbsp; var str = "";&nbsp; for(var i=0;i<A.length;i++){&nbsp; &nbsp; &nbsp; str = "";&nbsp; &nbsp; &nbsp; for(var j=0;j<B.length;j++){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(A[i]==B[j]){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; str = A[i]//相等把 值 加进数组&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; arr.push(str)&nbsp; }return arr;}console.log(func(A,B))//["","b",""]
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript