gpt4 book ai didi

javascript - 用 JavaScript 替换 JSON 中的字符串

转载 作者:行者123 更新时间:2023-11-28 04:44:55 24 4
gpt4 key购买 nike

我有一个简单的 JSON,我想在其中替换一个属性中的一个字符串。假设这是我的 JSON:

var values = [
{ id: 10, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.256248, latitude: 19347774275 },
{ id: 11, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.7534025, latitude: 19347868 },
{ id: 12, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.85458, latitude: 1934748775 },
{ id: 13, url: "www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG", longitude: 121.5525, latitude: 19317868 }
]

现在我想做的是用斜杠 / 替换每个 \ 。例如:

来自网址:www.mydomainname.com/images\uploads\xfolder\FILE_NAME.JPG到网址:www.mydomainname.com/images/uploads/xfolder/FILE_NAME.JPG

我尝试的很简单。这是我试过的:

for(i = 0; i < values.length; i++){
var str = values[i].url;
var res = str.replace("''\''", "/");
}

但是没有用。任何帮助将不胜感激,因为我已经有一段时间被困住了提前致谢:)

最佳答案

你目前从未对 res 做任何事情。请改用带有全局标志的正则表达式:

var values=[{id:10,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.256248,latitude:19347774275},{id:11,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.7534025,latitude:19347868},{id:12,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.85458,latitude:1934748775},{id:13,url:"www.mydomainname.com/images\\uploads\\xfolder\\FILE_NAME.JPG",longitude:121.5525,latitude:19317868}];
values.forEach(value => value.url = value.url.replace(/\\/g, '/'));
console.log(values);

关于javascript - 用 JavaScript 替换 JSON 中的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50615481/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com