为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

狐的传说 2022-06-09 19:01:02
我有一个地址并想更改一些元素,我的地址在下面: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.
查看完整描述

1 回答

?
开心每一天1111

TA贡献1836条经验 获得超13个赞

我假设您不知道地址的最后一位是什么


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)


查看完整回答
反对 回复 2022-06-09
  • 1 回答
  • 0 关注
  • 172 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号