从数组中删除最后一项

JavaScript

宝儿A梅

2020-03-16

我有以下数组。

var arr = [1,0,2];

我想删除最后一个元素,即2。

我用过,arr.slice(-1);但不会删除值。

第1848篇《从数组中删除最后一项》来自Winter(https://github.com/aiyld/aiyld.github.io)的站点

20个回答
神乐十三 2020.03.16
var stack = [1,2,3,4,5,6];

stack.reverse().shift();

stack.push(0);

Output will be: Array[0,1,2,3,4,5]. This will allow you to keep the same amount of array elements as you push a new value in.

Near米亚 2020.03.16

If you want to remove n item from end of array in javascript, you can easily use:

arr.splice(-n, n);
EvaMandy 2020.03.16

This is good way to remove last item :

if (arr != null && arr != undefined && arr.length > 0) {
      arr.splice(arr.length - 1, 1);
}

Detail of splice as following:

splice(startIndex, number of splice)

GilLEY 2020.03.16

say you have var arr = [1,0,2]

arr.splice(-1,1) will return to you array [1,0]; while arr.slice(-1,1) will return to you array [2];

Sam神奇A 2020.03.16

2019 ECMA5 Solution:

const new_arr = arr.reduce((d, i, idx, l) => idx < l.length - 1 ? [...d, i] : d, [])

Non destructive, generic, one-liner and only requires a copy & paste at the end of your array.

猿小胖 2020.03.16
var a = [1,2,3,4,5,6]; 
console.log(a.reverse().slice(1).reverse());
//Array(5) [ 1, 2, 3, 4, 5 ]
L凯 2020.03.16

splice(index,howmany)-这个解决方案听起来不错。但这种的howmany只会工作的积极数组索引。要删除最后两个项目或三个项目,请使用索引本身。

例如,使用splice(-2)删除最后两个项目。splice(-3)用于删除最后三个项目。

斯丁 2020.03.16

值得一提的是,slice都将返回一个新的数组,而.pop().splice()会产生变异的现有阵列。

如果您喜欢使用链式命令样式处理数据集合,那么您将真的想坚持slice这样的事情。

例如:

myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var newArrayOfThings = myArray
  .filter(x => x > 5)              // only bigly things
  .slice(-1)                       // get rid of the last item
  .map(x => `The number is: ${x}`);// map to a set of strings

它可以需要更多的折腾了,和可变管理,做同一种用“流行”的东西,因为不像mapfilter等等,你没有得到一个新的阵列回来。

与的情况相同push,后者会在数组的末尾添加一个项目。这样做可能会更好,concat因为这样可以使流程继续进行。

myArray = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];

var newArrayOfThings = myArray
  .filter(x => x > 5)              // only bigly things
  .slice(-1)                       // get rid of the "10"
  .concat([100])                   // note with concat, you must pass an array
  .map(x => `The number is: ${x}`) // map to a set of strings
  

神奇神奇Near 2020.03.16

//声明数组

var names = [“ harry”,“ aahan”,“ arav”,“ you”];

//此命令将删除的最后一个元素 names

names.pop();

村村Pro 2020.03.16
var arr = [1,0,2];
arr.length--; 

//删除最后一个元素//需要检查arr.length> 0

阿良 2020.03.16

此方法对于删除和存储数组的最后一个元素更有用。

var sampleArray = [1,2,3,4];// Declaring the array
var lastElement = sampleArray.pop();//this command will remove the last element of `sampleArray` and stores in a variable called `lastElement` so that you can use it if required.

现在的结果是:

console.log(sampleArray); //This will give you [1,2,3]
console.log(lastElement); //this will give you 4
Stafan卡卡西 2020.03.16

arr.slice(-1)将返回数组最后一个元素的副本,但保留原始数组不变。

要从n数组中删除最后一个元素,请使用arr.splice(-n)(请注意“ splice”中的“ p”)。返回值将是一个包含删除的元素的新数组。

更简单的是,对n == 1,用val = arr.pop()

西门Near 2020.03.16

只需在您的用例中使用以下内容:

arr.pop()
Itachi伽罗 2020.03.16

我认为.pop()这是最“正确”的解决方案,但是有时可能无法正常工作,因为您需要在没有最后一个元素的情况下使用数组。

在这种情况下,您可能需要使用以下内容,它将返回 [1,2,3]

var arr = [1,2,3,4];
console.log(arr.splice(0,arr.length-1));

虽然.pop()会返回4

var arr = [1,2,3,4];
console.log(arr.pop());

这可能不是理想的...

希望这可以节省您一些时间。

宝儿猪猪西门 2020.03.16

您可以使用splice()以下两种方式进行操作

  1. arr.splice(-1,1)
  2. arr.splice(arr.length-1,1)

splice(position_to_start_deleting, how_many_data_to_delete) 接受两个参数。

position_to_start_deleting:从零开始的索引,从此处开始删除。 how_many_data_to_delete:应从指示的索引中删除多少个连续数据。

您也可以使用pop()as pop()删除最后一个元素,因为as 可以从某个数组中删除最后一个元素。
采用arr.pop()

番长Gil 2020.03.16

您可以简单地使用, arr.pop()

这将删除数组的最后一个条目。

var arr = [1,0,2]; 
var popped = arr.pop();//Now arr = [1,0] & popped = 2
卡卡西 2020.03.16

有一个功能,在这里解释

arr.pop();
卡卡西卡卡西 2020.03.16

通过示例学习:

let array_1 = [1,2,3,4];
let array_2 = [1,2,3,4];
let array_3 = [1,2,3,4];

array_1.splice(-1,1)  // output --> [4]      array_1 = [1,2,3]
array_2.slice(0,-1);  // output --> [1,2,3]  array_2 = [1,2,3,4]
array_3.pop();        // output --> 4        array_3 = [1,2,3]
Sam小哥逆天 2020.03.16

您将需要执行此操作,因为slice这不会更改原始数组。

arr = arr.slice(-1);

如果要更改原始数组,可以使用splice

arr.splice(-1, 1);

pop

arr.pop();
L神无Mandy 2020.03.16

使用拼接(索引,多个)

arr.splice(-1,1)

问题类别

JavaScript Ckeditor Python Webpack TypeScript Vue.js React.js ExpressJS KoaJS CSS Node.js HTML Django 单元测试 PHP Asp.net jQuery Bootstrap IOS Android