Javascript 删除数组中的第一个和最后一个元素

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4644139/
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 13:25:22  来源:igfitidea点击:

to remove first and last element in array

javascriptarrays

提问by Mohan Ram

How to remove first and last element in an array?

如何删除数组中的第一个和最后一个元素?

For example:

例如:

var fruits = ["Banana", "Orange", "Apple", "Mango"];

Expected output array:

预期输出数组:

["Orange", "Apple"]

回答by

fruits.shift();  // Removes the first element from an array and returns only that element.
fruits.pop();    // Removes the last element from an array and returns only that element. 

See all methods for an Array.

查看数组的所有方法。

回答by Anurag

Creates a 1 level deep copy.

创建 1 级深副本。

fruits.slice(1, -1)

Let go of the original array.

放开原来的数组。

Thanks to @Tim for pointing out the spelling errata.

感谢@Tim 指出拼写错误。

回答by Tipu

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var newFruits = fruits.slice(1, -1);
console.log(newFruits); //  ["Orange", "Apple"];

Here, -1 denotes the last element in an array and 1 denotes the second element.

这里,-1 表示数组中的最后一个元素,1 表示第二个元素。

回答by Chinokao

I use splice method.

我使用拼接方法。

fruits.splice(0, 1); // Removes first array element

var lastElementIndex = fruits.length-1; // Gets last element index

fruits.splice(lastElementIndex, 1); // Removes last array element

To remove last element also you can do it this way:

要删除最后一个元素,您也可以这样做:

fruits.splice(-1, 1);

See Remove last item from arrayto see more comments about it.

请参阅从数组中删除最后一项以查看有关它的更多评论。

回答by Harunur Rashid

push()adds a new element to the end of an array.
pop()removes an element from the end of an array.

push()在数组末尾添加一个新元素。
pop()从数组的末尾删除一个元素。

unshift()adds a new element to the beginning of an array.
shift()removes an element from the beginning of an array.

unshift()在数组的开头添加一个新元素。
shift()从数组的开头删除一个元素。

To remove first element from an array arr, use arr.shift()
To remove last element from an array arr, use arr.pop()

要从数组中删除第一个元素arr,请使用arr.shift()
要从数组中删除最后一个元素arr,请使用arr.pop()

回答by Yosvel Quintero Arguelles

You can use Array.prototype.reduce().

您可以使用Array.prototype.reduce()

Code:

代码:

const fruits = ['Banana', 'Orange', 'Apple', 'Mango'],
      result = fruits.reduce((a, c, i, array) => 0 === i || array.length - 1 === i ? a : [...a, c], []);

console.log(result);

回答by Penny Liu

This can be done with lodash _.tailand _.dropRight:

这可以通过 lodash_.tail_.dropRight

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(_.dropRight(_.tail(fruits)));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

回答by Richie

To remove element from array is easy just do the following

从数组中删除元素很容易,只需执行以下操作

let array_splited = [].split('/');
array_splited.pop()
array_splited.join('/')

回答by Labib Hussain

Say you have array named list. The Splice()function can be used for both adding and removing item in that array in specific index i.e that can be in the beginning or in the end or at any index. On the contrary there are another function name shift()and pop()which is capable of removing only the first and last item in the array.

假设您有名为 list 的数组。的剪接()函数可以被用于添加并在特定索引即阵列,可以是在开始时或在结束时或在任何索引中移除项。相反,还有另一个函数名shift()pop(),它们只能删除数组中的第一项和最后一项。

This is the Shift Function which is only capable of removing the first element of the array

这是 Shift 函数,它只能删除数组的第一个元素

var item = [ 1,1,2,3,5,8,13,21,34 ]; // say you have this number series 
item.shift(); // [ 1,2,3,5,8,13,21,34 ];

The Pop Function removes item from an array at its last index

Pop 函数从数组的最后一个索引处删除项目

item.pop(); // [ 1,2,3,5,8,13,21 ];

Now comes the splice function by which you can remove item at any index

现在来了 splice 功能,您可以通过它删除任何索引处的项目

item.slice(0,1); // [ 2,3,5,8,13,21 ] removes the first object
item.slice(item.length-1,1); // [ 2,3,5,8,13 ] removes the last object 

The slice function accepts two parameters (Index to start with, number of steps to go);

slice 函数接受两个参数(开始的索引要走的步数);

回答by vikas pal

var resident_array =  ["RC_FRONT", "RC_BACK", "RC_BACK"];
var remove_item = "RC_FRONT";
resident_array = $.grep(resident_array, function(value) {
            return value != remove_item;
    });
resident_array = ["RC_BACK", "RC_BACK"];