+ 2
How do I use .replace() in arrays?
var myArray = ["blue dog", "red dog", "yellow dog"]; Now from myArray, I want to replace every "dog" to "cat". How do I do that?
2 Réponses
+ 5
var out = myArray.map( a => a.replace("dog","cat") );
+ 3
replace is String.prototype.replace()
You have to call with an instance of string.
if you are not familiar with Array ES6 method forEach, use traditional for loop to loop through the array of strings.
replace also takes its second argument in function form.