Home > Back-end >  Combine array in JavaScript with underscore character
Combine array in JavaScript with underscore character

Time:02-15

 Var a = "837297,870895"
 Var b = "37297,36664"

OutPut = "837297_37297,870895_36664

I tried multiple script but does not working

CodePudding user response:

You can split and map the splitted array as such:

var a = "837297,870895" 
var b = "37297,36664"

var output = a.split(',').map((e,i)=> e "_" b.split(',')[i])
console.log(output.toString())

For ES2015 or lower

var a = "837297,870895" 
var b = "37297,36664"

var output = a.split(',').map(function(e,i){return e "_" b.split(',')[i]})
console.log(output.toString())

CodePudding user response:

const a = "837297,870895";
const b = "37297,36664";

const [c,d] = [a,b].map(e => e.split(','));

const output = c.map((e,i) => `${e}_${d[i]}`).join();

console.log( output );

  • Related