最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Remove the last item from an array (and not return item) - Stack Overflow

programmeradmin4浏览0评论

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

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

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

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

Is there a JavaScript function for this?

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

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

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

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

Is there a JavaScript function for this?

Share Improve this question asked Nov 30, 2013 at 19:51 user1469270user1469270 1
  • 1 Possible duplicate of Remove last item from array – Durgpal Singh Commented Jul 23, 2018 at 12:58
Add a comment  | 

7 Answers 7

Reset to default 8

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

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

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;
}

in an expression, using the comma operator.
( documentation: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.prototype.removeLast = function () {
       this.length--;
       return this;
 }

called with

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

which you can daisy chain :

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

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; });

Splice and pop will both return the removed element. They will mutate the original array, but if you are looking for a function that returns the array without the removed element, use slice. This way you can chain calls.

let abc = ['a', 'b', 'c', 'd'];
abc = abc.slice(0, -1)
  .map(value => value += value);
console.log(abc);
// prints ['aa', 'bb', 'cc']

for change a default behavior of pop() method in javascript you have to change prototype of pop method
Array.prototype.pop = function() {
    this.length = this.length - 1
    return this
}
const abc = ['a', 'b', 'c', 'd'];
console.log(abc.pop());
also you can make another Array method like removeLast with your custom oputput:
Array.prototype.removeLast = function() {
    this.pop()
    return this
}

if you dont want to change the output globally it's better to log your data after pop()

   const abc = ['a', 'b', 'c', 'd'];
   abc.pop()
   console.log(abc);
发布评论

评论列表(0)

  1. 暂无评论