Home > Software engineering >  Replace dash (-) in number array in javascript and make them into string array
Replace dash (-) in number array in javascript and make them into string array

Time:02-02

i wanna make this aarray

let number = [022-123-456-2322, 021-123-456-2322, 031-123-456-2377, 041-123-456-2322, ];

to this

let number = [0221234562322, 0211234562322, 0311234562377, 0411234562322, ];

and make them to string array

let number = ["0221234562322, 0211234562322, 0311234562377, 0411234562322" ];

CodePudding user response:

You have an issue with this already. Your first array is not possible, as if you console.log it, it actually is:

let number = [-2883, -2884, -2931, -2868]

You need the first array to contain those numbers in string form, not as integers. This must be done in the code you already have. After that's done, then you can remove the '-' symbols from the strings, using .replace() or something similar.

CodePudding user response:

your first array has to already be an array of strings otherwise you get an array as [ -2883, -2884, -2931, -2868 ] because something like 041-123-456-2322 is not a valid instance of type number !

So you would have :

let number = ["022-123-456-2322", "021-123-456-2322", "031-123-456-2377", "041-123-456-2322"]

You can map over the number array with the following...

number = number.map(a => a.split("-").join(""))

... to get your array of :

number = ["0221234562322", "0211234562322", "0311234562377", "0411234562322" ]
  •  Tags:  
  • Related