从数组中删除最后一项(而不是返回项)

时间:2022-06-13 17:32:19

My current array: abc = ['a', 'b', 'c', 'd'];

我目前的数组:abc = ['a','b','c','d'];

I understand .pop() remove and returns the last item of an array, thus: abc.pop(); = 'd'

我理解.pop()删除并返回数组的最后一项,因此:abc.pop(); ='d'

However, I want to remove the last item, and return the array. So it would return:

但是,我想删除最后一项,并返回该数组。所以会回来:

['a', 'b', 'c'];

['a','b','c'];

Is there a JavaScript function for this?

这有JavaScript功能吗?

5 个解决方案

#1


3  

pop() function also removes last element from array, so this is what you want(Demo on JSFiddle):

pop()函数也从数组中删除最后一个元素,所以这就是你想要的(在JSFiddle上演示):

var abc = ['a', 'b', 'c', 'd'];
abc.pop()
alert(abc); // a, b, c

#2


2  

Array.prototype.popAndReturnArray = function( ){
   this.pop();
   return this;
}

#3


2  

in an expression, using the comma operator.
( documentation:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator // thx @Zero )

在表达式中,使用逗号运算符。 (文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator // thx @Zero)

(abc.length--, abc)    

// expl :
  (abc.length--, abc).sort();

Or in a function, most handy being to set it on Array prototype :

或者在函数中,最方便的是将它设置在Array原型上:

 Array.prototype.removeLast = function () {
       this.length--;
       return this;
 }

called with

打电话给

var abc = ['you', 'and', 'me'];
abc.removeLast(); 

which you can daisy chain :

你可以菊花链:

abc.removeLast().sort();

#4


1  

Do this

做这个

abc = abc.splice(0, abc.length-1)

Edit: It has been pointed out that this actually returns a new array(albeit with the same variable name).

编辑:已经指出这实际上返回一个新数组(尽管具有相同的变量名)。

If you want to return the same array, you'll have to make your own function

如果要返回相同的数组,则必须自己创建函数

function popper(arr) {
   arr.pop();
   return arr;
}

#5


0  

Yes, what you want is more generally the "filter" function.

是的,你想要的更通常是“过滤”功能。

It takes a function and returns everything that passes a test function, here's an example:

它需要一个函数并返回通过测试函数的所有内容,这是一个例子:

abc = ['a', 'b', 'c', 'd'];

abc.filter(function(member,index) { return index !== abc.length - 1; });

#1


3  

pop() function also removes last element from array, so this is what you want(Demo on JSFiddle):

pop()函数也从数组中删除最后一个元素,所以这就是你想要的(在JSFiddle上演示):

var abc = ['a', 'b', 'c', 'd'];
abc.pop()
alert(abc); // a, b, c

#2


2  

Array.prototype.popAndReturnArray = function( ){
   this.pop();
   return this;
}

#3


2  

in an expression, using the comma operator.
( documentation:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator // thx @Zero )

在表达式中,使用逗号运算符。 (文档:https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator // thx @Zero)

(abc.length--, abc)    

// expl :
  (abc.length--, abc).sort();

Or in a function, most handy being to set it on Array prototype :

或者在函数中,最方便的是将它设置在Array原型上:

 Array.prototype.removeLast = function () {
       this.length--;
       return this;
 }

called with

打电话给

var abc = ['you', 'and', 'me'];
abc.removeLast(); 

which you can daisy chain :

你可以菊花链:

abc.removeLast().sort();

#4


1  

Do this

做这个

abc = abc.splice(0, abc.length-1)

Edit: It has been pointed out that this actually returns a new array(albeit with the same variable name).

编辑:已经指出这实际上返回一个新数组(尽管具有相同的变量名)。

If you want to return the same array, you'll have to make your own function

如果要返回相同的数组,则必须自己创建函数

function popper(arr) {
   arr.pop();
   return arr;
}

#5


0  

Yes, what you want is more generally the "filter" function.

是的,你想要的更通常是“过滤”功能。

It takes a function and returns everything that passes a test function, here's an example:

它需要一个函数并返回通过测试函数的所有内容,这是一个例子:

abc = ['a', 'b', 'c', 'd'];

abc.filter(function(member,index) { return index !== abc.length - 1; });