I'm trying to replace values from an array which are present in a string with another array. What is the best way to do this in Javascript? here is my code :
var string = "I've to but the Item1 along with Item2 and Item3. From the Item4."
var array1 = ['Item1', 'Item2', 'Item3', 'Item4']
var array2 = ['Product1', 'Product2', 'Product3', 'Product4']
and output should be like this
var replaced = "I've to but the Product1 along with Product2 and Product3. From the Product4."
every value in both arrays is completely different from each other.
You can replace the string using String.prototype.replaceAll