Javascript - Pop a value from array, but not at the end of array
To pop the first one off, use:
first = array.shift();
To pop any other one off, use:
removed = array.splice(INDEX, 1)[0];
You're looking for splice
. Example: http://jsbin.com/oteme3:
var a, b;
a = [1, 2, 3, 15, 20, 12, 14, 18, 7, 8];
display("a.length before = " + a.length);
b = a.splice(3, 1);
display("a.length after = " + a.length);
display("b[0] = " + b[0]);
...displays "a.length before = 10", then "a.length after = 9", then "b[0] = 15"
Note that splice
returns an array of the removed values rather than just one, but that's easily handled. It's also convenient for inserting values into an array.