Home > Software engineering >  Changing specific array value in javascript
Changing specific array value in javascript

Time:09-07

let bdays = ["10-17", "05-19", "20-19"];

how do i change the '-' in that array to '/'.

CodePudding user response:

You could also use Array.map to create a new array of formatted values

let bdays = ["10-17", "05-19", "20-19"];``
let formattedBdays = bdays.map((date) => { 
    return date.replace('-','/')
});
console.log(formattedBdays);

Gives Output

['10/17', '05/19', '20/19']

You can learn more about map in the docs here

CodePudding user response:

let bdays = ["10-17", "05-19", "20-19"];``
for( let bday of bdays) {
    bdays[bdays.indexOf(bday)]= bday.replace('-','/');
}
  • Related