UNPKG

phpjs

Version:

php.js offers community built php functions in javascript

104 lines (91 loc) 3.33 kB
--- layout: page title: "JavaScript array_pop function" comments: true sharing: true footer: true alias: - /functions/view/array_pop:329 - /functions/view/array_pop - /functions/view/329 - /functions/array_pop:329 - /functions/329 --- <!-- Generated by Rakefile:build --> A JavaScript equivalent of PHP's array_pop {% codeblock array/array_pop.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_pop.js raw on github %} function array_pop (inputArr) { // From: http://phpjs.org/functions // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + bugfixed by: Brett Zamir (http://brett-zamir.me) // + input by: Theriault // % note 1: While IE (and other browsers) support iterating an object's // % note 1: own properties in order, if one attempts to add back properties // % note 1: in IE, they may end up in their former position due to their position // % note 1: being retained. So use of this function with "associative arrays" // % note 1: (objects) may lead to unexpected behavior in an IE environment if // % note 1: you add back properties with the same keys that you removed // * example 1: array_pop([0,1,2]); // * returns 1: 2 // * example 2: data = {firstName: 'Kevin', surName: 'van Zonneveld'}; // * example 2: lastElem = array_pop(data); // * returns 2: 'van Zonneveld' // * results 2: data == {firstName: 'Kevin'} var key = '', lastKey = ''; if (inputArr.hasOwnProperty('length')) { // Indexed if (!inputArr.length) { // Done popping, are we? return null; } return inputArr.pop(); } else { // Associative for (key in inputArr) { if (inputArr.hasOwnProperty(key)) { lastKey = key; } } if (lastKey) { var tmp = inputArr[lastKey]; delete(inputArr[lastKey]); return tmp; } else { return null; } } } {% endcodeblock %} - [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_pop.js) Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are the closest match to this hashtable-like data structure. Please also note that php.js offers community built functions and goes by the [McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online functions that are far from perfect, in the hopes to spark better contributions. Do you have one? Then please just: - [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/array_pop.js) ### Example 1 This code {% codeblock lang:js example %} array_pop([0,1,2]); {% endcodeblock %} Should return {% codeblock lang:js returns %} 2 {% endcodeblock %} ### Example 2 This code {% codeblock lang:js example %} data = {firstName: 'Kevin', surName: 'van Zonneveld'}; lastElem = array_pop(data); {% endcodeblock %} Should return {% codeblock lang:js returns %} 'van Zonneveld' {% endcodeblock %} ### Other PHP functions in the array extension {% render_partial _includes/custom/array.html %}