首页 > 解决方案 > Javascript替换在数组字符串元素中不起作用

问题描述

这是我的数组:

let model = ["xxx","xxx"];

我想用'o'替换数组第二个元素的字符串中的第二个'x'。

我试过这个:

let model = ["xxx", "xxx"];

console.log(model[1][2])
// -> output : x -> OK

model[1][2] = model[1][2].replace('x', 'o')

console.log(model);
// -> output : ["xxx", "xxx"] -> ???

我也试过这个:

let model = ["xxx", "xxx"];

console.log(model[1][2])
// -> output : x

model[1][2] = 'o'

console.log(model);
// -> output : ["xxx", "xxx"]

为什么不工作?谢谢 !

标签: javascriptstringreplace

解决方案


推荐阅读