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

OutPut = "837297_37297,870895_36664

I tried multiple script but does not working

Heretic Monkey
  • 11,687
  • 7
  • 53
  • 122
Rahul
  • 11
  • 3
  • 2
    split and a loop. Show what you attempted – epascarello Feb 14 '22 at 20:59
  • 2
    JavaScript is case sensitive `Var` is not `var` (lowercase) – Mister Jojo Feb 14 '22 at 21:01
  • Your question is not combining arrays. Your question is about replacing the comma with an underscore and then concatenating the strings with a comma between. – Heretic Monkey Feb 14 '22 at 21:33
  • Does this answer your question? [replace one string with another in javascript](https://stackoverflow.com/questions/17170841/replace-one-string-with-another-in-javascript) and [Joining two strings with a comma and space between them](https://stackoverflow.com/q/20881950/215552) – Heretic Monkey Feb 14 '22 at 21:38

2 Answers2

0

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())
JaivBhup
  • 792
  • 4
  • 7
  • @jaivBhp Getting error - Error at line 6, character 31: This language feature is only supported for ECMASCRIPT_2015 mode or better: arrow function. – Rahul Feb 14 '22 at 21:08
  • 2
    Rahul, it's 2022. Maybe time to update your development profile. – Andy Feb 14 '22 at 21:13
  • Yes but you can also not use the arrow function. But i suggest to update you javascript to latest ecmascript – JaivBhup Feb 14 '22 at 21:14
  • 1
    Better yet, understand what the functions do, so that you can do them without the specific functionality available. – Heretic Monkey Feb 14 '22 at 21:41
-1

You can start by converting the two strings (lists) into arrays using .map() and .split() methods. Then use .map() and .join() methods to produce the desired string (list):

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 );
PeterKA
  • 24,158
  • 5
  • 26
  • 48