JavaScript array_shift
Pops an element off the beginning of the array
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 33 34 3536 37 38 39 4041 42 43 44 4546 47 48 49 5051 52 53 54 5556 57 58 59 6061 62 63 64 65 | function array_shift (inputArr) { // Pops an element off the beginning of the array // // version: 1008.1718 // discuss at: http://phpjs.org/functions/array_shift // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Martijn Wieringa // % note 1: Currently does not handle objects // * example 1: array_shift(['Kevin', 'van', 'Zonneveld']); // * returns 1: 'Kevin' var props = false, shift = undefined, pr = '', allDigits = /^\d$/, int_ct=-1, _checkToUpIndices = function (arr, ct, key) { // Deal with situation, e.g., if encounter index 4 and try to set it to 0, but 0 exists later in loop (need to // increment all subsequent (skipping current key, since we need its value below) until find unused) if (arr[ct] !== undefined) { var tmp = ct; ct += 1; if (ct === key) { ct += 1; } ct = _checkToUpIndices(arr, ct, key); arr[ct] = arr[tmp]; delete arr[tmp]; } return ct; }; if (inputArr.length === 0) { return null; } if (inputArr.length > 0) { return inputArr.shift(); } /* UNFINISHED FOR HANDLING OBJECTS for (pr in inputArr) { if (inputArr.hasOwnProperty(pr)) { props = true; shift = inputArr[pr]; delete inputArr[pr]; break; } } for (pr in inputArr) { if (inputArr.hasOwnProperty(pr)) { if (pr.search(allDigits) !== -1) { int_ct += 1; if (parseInt(pr, 10) === int_ct) { // Key is already numbered ok, so don't need to change key for value continue; } _checkToUpIndices(inputArr, int_ct, pr); arr[int_ct] = arr[pr]; delete arr[pr]; } } } if (!props) { return null; } return shift; */ } |
Examples
Running
1 | array_shift(['Kevin', 'van', 'Zonneveld']); |
Should return
1 | 'Kevin' |
Dependencies
No dependencies, you can use this function standalone.
Open syntax issues
php.js uses JsLint to help us keep our code consistent and prevent some common bugs.
Eventually we want all code to pass or at least take into consideration most fixes suggested by JsLint, following this JsLint configuration we’ve decided on.
Authors
Thanks to the following developers, you get to have array_shift goodness in JavaScript.
It turns out that 'shift' and 'unshift' functions are supported by JS itself (tested in IE 6 and FF 2)
function array_shift(f_array)
{
if(f_array.length > 0)
{
return f_array.shift();
}
return null;
}
function array_unshift(f_array)
{
for(var i = 1; i < array_unshift.arguments.length; i++)
{
f_array.unshift(array_unshift.arguments[i]);
}
return (array_unshift.arguments.length - 1);
}
@ Buzz: I do not agree. The original array is changed by reference. But the function should return what has been removed: the value of the first element.
@ Post cannot have an empty name: I agree that this function could probably be trimmed down a bit, but your version does not support associative arrays, so for now I'm going to have to stick with the current version. Do you agree? Thanks for your input, and if you have a different opinion or code, let me know!


Kevin van Zonneveld
31 May '08