Edit: as per OP's subsequent comments, it looks like you need one of my last two options below (I leave the rest here for educational purposes).
N ( 2 ), , , N N . , ( ).
, .splice() .slice(). .splice() . .slice() .
.splice() .slice().
( , ):
stack.splice(0, stack.length -2);
var lastElements = stack.splice(-2, 2);
var lastElements = stack.slice(-2);
N , , , :
var collection = [];
for (i = stack.length - 1; i >= 0; i--) {
if (stack[i] !== undefined) {
collection.unshift(stack[i]);
if (collection.length == 2) {
break;
}
}
}
IE8 polyfill Array.prototype.filter, , :
stack = stack.filter(function(item) {return item !== undefined}).slice(-2);
: http://jsfiddle.net/jfriend00/f7LrA/