javascript - Combine each element of a string with the second string -
i have 2 strings
[a,b,c] //there number of items in these 2 strings [x,y,z]
i want output this
a[x,y,z] b[x,y,z] c[x,y,z]
not quite able logic output.
to array use map()
var s1 = '[a,b,c]', s2 = '[x,y,z]'; console.log( s1 .slice(1, -1) // remove `[` , `]` .split(',') // split based on `,` .map(function(a) { // iterate , generate array return + s2; }) )
to string use reduce()
var s1 = '[a,b,c]', s2 = '[x,y,z]'; console.log( s1 .slice(1, -1) // remove `[` , `]` .split(',') // split based on `,` .reduce(function(a, b) { // iterate , generate string return + (a.length ? '\n' : ' ') + b + s2; }, '') )
Comments
Post a Comment