2

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?

  • 1
    Possible duplicate of [Remove last item from array](https://stackoverflow.com/questions/19544452/remove-last-item-from-array) – Durgpal Singh Jul 23 '18 at 12:58

7 Answers7

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
Daniel Kmak
  • 18,164
  • 7
  • 66
  • 89
4

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;
}
tewathia
  • 6,890
  • 3
  • 22
  • 27
2

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();
GameAlchemist
  • 18,995
  • 7
  • 36
  • 59
  • Adedded plus, 'cause was the same approach I was going to suggest. However, a link to [comma operator](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Comma_Operator) documentation would be nice in the answer too, for the readers doesn't know how it works – the explanation in the code is not sufficient, IMVHO. – ZER0 Nov 30 '13 at 20:12
1
Array.prototype.popAndReturnArray = function( ){
   this.pop();
   return this;
}
Saad Ahmed
  • 1,077
  • 9
  • 9
1

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; });
DataDao
  • 703
  • 6
  • 12
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']
gyleg5
  • 144
  • 1
  • 2
  • 11
0
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);
Kaveh Karami
  • 335
  • 1
  • 4
  • 14