JavaScript - 获取数组中除最后一项之外的所有内容

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3214409/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 03:42:11  来源:igfitidea点击:

JavaScript - Get all but last item in array

javascriptdom

提问by balexander

This is my code:

这是我的代码:

 function insert(){
  var loc_array = document.location.href.split('/');
  var linkElement = document.getElementById("waBackButton");
  var linkElementLink = document.getElementById("waBackButtonlnk");
  linkElement.innerHTML=loc_array[loc_array.length-2];
  linkElementLink.href = loc_array[loc_array.length];
 }

I want linkElementLink.hrefto grab everything but the last item in the array. Right now it is broken, and the item before it gets the second-to-last item.

我想linkElementLink.href抓取除数组中最后一项之外的所有内容。现在它坏了,它之前的项目获得倒数第二个项目。

回答by Gumbo

I'm not quite sure what you're trying to do. But you can use sliceto slice the array:

我不太确定你要做什么。但是您可以使用slice对数组进行切片:

loc_array = loc_array.slice(0, -1);

回答by bobince

Use pathnamein preference to hrefto retrieve only the path part of the link. Otherwise you'll get unexpected results if there is a ?queryor #fragmentsuffix, or the path is /(no parent).

pathname优先使用href仅检索链接的路径部分。否则,如果有?query#fragment后缀,或者路径是/(没有父级),您将得到意想不到的结果。

linkElementLink.href= location.pathname.split('/').slice(0, -1).join('/');

(But then, surely you could just say:)

(但是,你当然可以说:)

linkElementLink.href= '.';

Don't do this:

不要这样做:

linkElement.innerHTML=loc_array[loc_array.length-2];

Setting HTML from an arbitrary string is dangerous. If the URL you took this text from contains characters that are special in HTML, like <and &, users could inject markup. If you could get <script>in the URL (which you shouldn't be able to as it's invalid, but some browser might let you anyway) you'd have cross-site-scripting security holes.

从任意字符串设置 HTML 是危险的。如果您获取此文本的 URL 包含 HTML 中的特殊字符,例如<&,则用户可能会注入标记。如果你能进入<script>URL(你不应该能够进入,因为它是无效的,但一些浏览器可能会让你无论如何)你会有跨站点脚本安全漏洞。

To set the text of an element, instead of HTML, either use document.createTextNode('string')and append it to the element, or branch code to use innerText(IE) or textContent(other modern browsers).

要设置元素的文本而不是 HTML,请使用document.createTextNode('string')并将其附加到元素,或使用分支代码innerText(IE) 或textContent(其他现代浏览器)。

回答by Brian M. Hunt

If using lodashone could employ _.initial(array):

如果使用lodash可以使用_.initial(array)

_.initial(array): Gets all but the last element of array.

_.initial(array):获取除数组最后一个元素之外的所有元素。

Example:

例子:

_.initial([1, 2, 3]);
// → [1, 2]

回答by g.d.d.c

Depending on whether or not you are ever going to reuse the array you could simply use the pop()method one time to remove the last element.

根据您是否打算重用数组,您可以简单地使用该pop()方法一次删除最后一个元素。

回答by qw3n

linkElementLink.href = loc_array[loc_array.length];adds a new empty slot in the array because arrays run from 0 to array.length-1; So you returning an empty slot.

linkElementLink.href = loc_array[loc_array.length];在数组中添加一个新的空槽,因为数组从 0 运行到 array.length-1;所以你返回一个空槽。

linkElement.innerHTML=loc_array[loc_array.length-2];if you use the brackets you are only getting the contents of one index. I'm not sure if that is what you want? The next section tells how to get more than one index.

linkElement.innerHTML=loc_array[loc_array.length-2];如果您使用括号,您只会获得一个索引的内容。我不确定这是否是你想要的?下一节将介绍如何获取多个索引。

To get what you want you need for the .href you need to slice the array. linkElementLink.href = loc_array.slice(0,loc_array.length-1);or using a negative to count from the end linkElementLink.href = loc_array.slice(0,-1);and this is the faster way.

要获得 .href 所需的内容,您需要对数组进行切片。 linkElementLink.href = loc_array.slice(0,loc_array.length-1);或使用负数从末尾开始计数 linkElementLink.href = loc_array.slice(0,-1);,这是更快的方法。

Also note that when getting to stuff straight from the array you will get the same as the .toString()method, which is item1, item2, item3. If you don't want the commas you need to use .join(). Like array.join('-')would return item1-item2-item3. Here is a list of all the array methods http://www.w3schools.com/jsref/jsref_obj_array.asp. It is a good resource for doing this.

另请注意,当直接从数组获取内容时,您将获得与.toString()方法相同的内容,即 item1、item2、item3。如果您不想要逗号,则需要使用.join(). 就像array.join('-')会返回 item1-item2-item3。这是所有数组方法的列表http://www.w3schools.com/jsref/jsref_obj_array.asp。这是执行此操作的好资源。