【发布时间】:2018-03-27 15:47:48
【问题描述】:
我有一个在 JavaScript 中使用 Array.pop 函数的问题。 Array.pop 移除并返回 Array 的最后一个元素。那么我的问题是:是否可以删除并返回数组的最后一个 TWO 元素并返回它们而不是最后一个?
我正在使用这个函数来返回 URL 的最后一个元素,像这样:
网址:www.example.com/products/cream/handcreamproduct1
'url'.splice("/").pop(); -----> "handcreamproduct1"
我想要的是:
'url'.splice("/").pop(); -----> "cream/handcreamproduct1"
我想获取 url 中的最后两个参数并返回它们,使用 .pop 我只得到最后一个。请记住,URL 是动态长度。网址可能如下所示:
网址:www.example.com/products/cream/handcreamproduct1
或
网址:www.example.com/products/handcream/cream/handcreamproduct1
【问题讨论】:
标签: javascript arrays url