Use PHP functions in JavaScript

JavaScript key

Return the key of 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
44
4546
47
48
49
function key (arr) {
    // Return the key of the element currently pointed to by the internal array pointer  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/key    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +   input by: Riddler (http://www.frontierwebdev.com/)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Uses global: php_js to store the array pointer
    // *     example 1: array = {fruit1: 'apple', 'fruit2': 'orange'}    // *     example 1: key(array);
    // *     returns 1: 'fruit1'
    // 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 cursor = pointers[pointers.indexOf(arr)+1];
    if (!(arr instanceof Array)) {        var ct = 0;
        for (var k in arr) {
            if (ct === cursor) {
                return k;
            }            ct++;
        }
        return false; // Empty
    }
    if (arr.length === 0) {        return false;
    }
    return cursor;
}
external links: original PHP docs | raw js source

Examples

Running

1
2
array = {fruit1: 'apple', 'fruit2': 'orange'}
key(array);

Should return

1
'fruit1'

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 key goodness in JavaScript.

Comments

Add Comment
Use:
[CODE]
your_stuff('here');
[/CODE]
for proper code formatting
By submitting code here you are allowing us to use it in php.js hence dual licensing it under the MIT and GPL licenses

Gravatar
Brett Zamir
26 Dec '09 Permalink

q  @Riddler: Ouch, thanks for that. Had a bunch of other functions to fix; now done in Git. I'm really too spoiled working usually in a Firefox-only environment.
@Theriault: Just saw your comment now. Yes, thanks, I caught those, and also a number of others. All should be fixed now. Thank heavens for Notepad++'s find files feature (I know IE does support the string indexOf at least, so I avoided doing anything for those).

Gravatar
Theriault
26 Dec '09 Permalink

q  To elaborate on Riddler’s post, indexOf isn’t supported by Internet Explorer’s JScript, so this won’t work in any version of the browser, neither will the other functions: next, prev, pos, current, end, and reset.

Gravatar
Riddler
25 Dec '09 Permalink

q  > if (pointers.indexOf(arr) === -1) {

Error occurred in IE - Object doesn't support this property or method

Array object has no "indexOf" method.


Contribute a New function