Shijin TR
Shijin TR

Reputation: 7768

Remove old values from array javascript

I have a JavaScript array like below,

 var stack=array();
 stack[0]=1;
 stack[1]=10;
 stack[5]=5;
 ------------
 ------------
 stack[50]=15;
 stack[55]=5; etc

I only need last n elements from this array,I mean if n=2, Then my result like

 stack[0]=15;
 stack[1]=5;

How to unset old values from array using JavaScript keeping needed elements?

Upvotes: 4

Views: 4320

Answers (4)

jfriend00
jfriend00

Reputation: 707876

Edit: based on the OP's subsequent comments, it looks like you want one of my last two options below (I leave the others here for educational purposes).

To get the last N elements (lets use 2 in these examples), you have these options depending upon whether you want to remove everything else from the original array, remove just the last N from the original array or make a copy of the last N from the original array. And, it also depends upon whether your array is spare or not (which your example kind of looks like it might be).

Basically, your friends here are the array operations .splice() and .slice(). .splice() changes the original array. .slice() copies elements from the original array.

References for .splice() and .slice().

Here are some of your options (depending upon exactly what you want to do):

// remove all elements except the last 2 from the array
stack.splice(0, stack.length -2);

or

// return a new array of just the last two elements
// removes them from the original array too
var lastElements = stack.splice(-2, 2);

or

// return a copy of just the first two items in the array (
// leaves original array unchanged
var lastElements = stack.slice(-2);

or

If your array is sparse and you want to collect the last N elements that actually exist, then you can iterate over it, collecting elements that exist:

var collection = [];
for (i = stack.length - 1; i >= 0; i--) {
    if (stack[i] !== undefined) {
        collection.unshift(stack[i]);
        if (collection.length == 2) {
            break;
        }
    }
}

or

If you don't require IE8 support or will install a polyfill for Array.prototype.filter and you just want a copy of the last two elements, you can shorten the previous option to this:

stack = stack.filter(function(item) {return item !== undefined}).slice(-2);

Demo of all methods: http://jsfiddle.net/jfriend00/f7LrA/

Upvotes: 3

user229044
user229044

Reputation: 239462

You think your array doesn't use continuous keys, but it does. The values at the indices you have not explicitly assigned to are simply undefined.

You need to remove the undefined elements from your array, and then slice the result:

var stack = [];
var n=2;

stack[0]=1;
stack[1]=10;
stack[2]=5;
stack[7]=15;
stack[9]=5; 

// Here, stack == [1, 10, 5, undefined × 4, 15, undefined × 1, 5]

// map stack to a new array, omitting "falsy" values
stack = stack.filter(function (v) { return v; });

// Now stack == [1, 10, 5, 15, 5]

stack = stack.slice(stack.length - n , stack.length)

console.log(stack[0]); // 15
console.log(stack[1]); // 5

This omits any falsy value; if you want to remove only the undefined elements...

stack = stack.filter(function (v) { return v !== undefined; });

Upvotes: 1

Rajaprabhu Aravindasamy
Rajaprabhu Aravindasamy

Reputation: 67217

You have to use Array.slice() to achieve what you want in this context.

Try,

stack = stack.slice(stack.length - n , stack.length)

DEMO

Upvotes: 2

Fizer Khan
Fizer Khan

Reputation: 92875

You can use splice to change the array

stack.splice(2, stack.length - 2);

You can use slice to get new array

var newStack = stack.slice(0, 2);

Upvotes: 0

Related Questions