jquery - Remove old values from array javascript - Stack Overflow

admin2025-04-08  2

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?

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?

Share Improve this question edited Aug 12, 2015 at 14:47 Shijin TR asked Feb 26, 2014 at 4:44 Shijin TRShijin TR 7,78811 gold badges63 silver badges125 bronze badges
Add a ment  | 

4 Answers 4

Reset to default 3

Edit: based on the OP's subsequent ments, 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/jfriend00/f7LrA/

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

Try,

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

DEMO

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

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);
转载请注明原文地址:http://conceptsofalgorithm.com/Algorithm/1744127281a232520.html

最新回复(0)