Home > front end >  Implementation [[' a ', 'b'], [' n ', 'm'], [' 0 '
Implementation [[' a ', 'b'], [' n ', 'm'], [' 0 '

Time:04-23

Written in js, there is a great god help you

CodePudding user response:

 (function (arr) {
Var r=[];
for (var i=0; i{
R=(function (a, b) {
If (a. ength==0)
{
return b;
} else {
Var c=a, r=[];
For (var x=0; XFor (var y=0; YR.p ush (c + b [y] [x]).
}
}
Return r
}
}) (r, arr [I])
}
The console. The log (r.j oin (', '))
Return r
}) ([[' a ', 'b'], [' n ', 'm' and 'x', 'y'], [' 0 ', '1']])

CodePudding user response:

 const arr=[[' a ', 'b'], [' m ', 'n'], [0, 1]] 

The function doArr (arr) {
Let the result=arr. The shift ()
While (arr. Length) {
Const other=arr. The shift ()
Const newResult=[]
Result. The forEach (item=& gt; {
Other. ForEach (_item=& gt; {
NewResult. Push (item + '+ _item)
})
})
Result=newResult [...]
}
Return the result
}
Const result=doArr (arr)
The console. The log (result)


 arr for [[' a ', 'b'], [' m ', 'n'], [0, 1]] 
///"am0", "am1", "an0", "an1", "bm0", "bm1", "bn0", "bn1"]
Arr for arr=[[' a ', 'b'], [' m ', 'n'], [0, 1], [3, 4]]
///"am03 am04", ""," am13 ", "am14," "an03", "an04", "an13", "an14," "bm03", "bm04", "bm13," "bm14", "bn03", "bn04", "bn13", "bn14"]

CodePudding user response:

refer to the second floor gqkmiss response:
 const arr=[[' a ', 'b'], [' m ', 'n'], [0, 1]] 

The function doArr (arr) {
Let the result=arr. The shift ()
While (arr. Length) {
Const other=arr. The shift ()
Const newResult=[]
Result. The forEach (item=& gt; {
Other. ForEach (_item=& gt; {
NewResult. Push (item + '+ _item)
})
})
Result=newResult [...]
}
Return the result
}
Const result=doArr (arr)
The console. The log (result)


 arr for [[' a ', 'b'], [' m ', 'n'], [0, 1]] 
///"am0", "am1", "an0", "an1", "bm0", "bm1", "bn0", "bn1"]
Arr for arr=[[' a ', 'b'], [' m ', 'n'], [0, 1], [3, 4]]
///"am03 am04", ""," am13 ", "am14," "an03", "an04", "an13", "an14," "bm03", "bm04", "bm13," "bm14", "bn03", "bn04", "bn13", "bn14"]



This method can, no matter how many layer and does not require recursion
  • Related