如何更改数组中最后一个索引的内容?

我有一个地址并想更改一些元素,我的地址在下面:


var address = "http://20.0.1.8/x-manufacturer/senders/d7aa5a30-681d-4e72-92fb-f0ba0f6f4c3e/sample-data";


我想将var地址“sample=data”的最后一个索引的内容更改为“new-data”,我写了下面的代码来解决它,但没有给我我需要的东西!


let array_address = address.split('/');

var new address = array_address.splice(0,9,"new-data");//It just remove last index,didn't replace.


狐的传说
浏览 173回答 1
1回答

开心每一天1111

我假设您不知道地址的最后一位是什么let url = new URL("http://20.0.1.8/x-manufacturer/senders/d7aa5a30-681d-4e72-92fb-f0ba0f6f4c3e/sample-data")let path = url.pathname.split("/")path[path.length-1] = "new-data"url.pathname=path.join("/")console.log(url.href)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript