JavaScript current
Return the element currently pointed to by the internal array pointer
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 | function current (arr) { // Return the element currently pointed to by the internal array pointer // // version: 1001.2911 // discuss at: http://phpjs.org/functions/current // + original by: Brett Zamir (http://brett-zamir.me) // % note 1: Uses global: php_js to store the array pointer // * example 1: transport = ['foot', 'bike', 'car', 'plane']; // * example 1: current(transport); // * returns 1: 'foot' // BEGIN REDUNDANT this.php_js = this.php_js || {}; this.php_js.pointers = this.php_js.pointers || []; var indexOf = function (value) { for (var i = 0, length=this.length; i < length; i++) { if (this[i] === value) { return i; } } return -1; }; // END REDUNDANT var pointers = this.php_js.pointers; if (!pointers.indexOf) { pointers.indexOf = indexOf; } if (pointers.indexOf(arr) === -1) { pointers.push(arr, 0); } var arrpos = pointers.indexOf(arr); var cursor = pointers[arrpos+1]; if (arr instanceof Array) { return arr[cursor] || false; } var ct = 0; for (var k in arr) { if (ct === cursor) { return arr[k]; } ct++; } return false; // Empty } |
Examples
Running
1 2 | transport = ['foot', 'bike', 'car', 'plane']; current(transport); |
Should return
1 | 'foot' |
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 current goodness in JavaScript.
No comments yet. Be the first!
spread the word:
Use any PHP function in JavaScript
These kind folks have already donated: Anonymous and Shawn Houser.
<your name here>