【发布时间】:2021-01-27 13:22:27
【问题描述】:
我正在尝试用另一个数组替换字符串中存在的数组中的值。在 Javascript 中执行此操作的最佳方法是什么? 这是我的代码:
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']
输出应该是这样的
var replaced = "I've to but the Product1 along with Product2 and Product3. From the Product4."
两个数组中的每个值都完全不同。
【问题讨论】:
-
你可以把你的例子设为minimal reproducible example吗?
-
这些变量的值是多少,
Item1...Item4和Product1...Product4 -
@JaromandaX 链接
-
链接,就像
http://example.com -
@JaromandaX 是的
标签: javascript arrays str-replace array-replace