Use PHP functions in JavaScript

JavaScript end

Advances array argument's internal pointer to the last element and return it

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
55
function end ( arr ) {
    // Advances array argument's internal pointer to the last element and return it  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/end    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   bugfixed by: Legaev Andrey
    // +    revised by: J A R
    // +   improved by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   restored by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)    // +    revised by: Brett Zamir (http://brett-zamir.me)
    // %        note 1: Uses global: php_js to store the array pointer
    // *     example 1: end({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});
    // *     returns 1: 'Zonneveld'
    // *     example 2: end(['Kevin', 'van', 'Zonneveld']);    // *     returns 2: 'Zonneveld'
    
    // 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);
    if (!(arr instanceof Array)) {
        var ct = 0;
        for (var k in arr) {            ct++;
            var val = arr[k];
        }
        if (ct === 0) {
            return false; // Empty        }
        pointers[arrpos+1] = ct - 1;
        return val;
    }
    if (arr.length === 0) {        return false;
    }
    pointers[arrpos+1] = arr.length - 1;
    return arr[pointers[arrpos+1]];
}
external links: original PHP docs | raw js source

Examples

» Example 1

Running

1
end({0: 'Kevin', 1: 'van', 2: 'Zonneveld'});

Should return

1
'Zonneveld'

» Example 2

Running

1
end(['Kevin', 'van', 'Zonneveld']);

Should return

1
'Zonneveld'

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 end 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
Itsacon
Jan 22nd Permalink

q  Ah, much better.

Strange, the problem cropped up when I updated my php.js, so maybe the version in the lib-build-tool is also still using the older one.

Thanks for the quick response (and the explanation)

Gravatar
Brett Zamir
Jan 22nd Permalink

q  @Itsacon, Thanks...The fix had already been applied a while ago (indexOf is supported in Firefox, etc., so that was the reason for the mistake). Anyhow, you can see the current version if you click "raw js source". Btw, the reason for the rest of the "convoluted" code is to support tracking of the array "pointer" (as in PHP). end() moves the pointer to the end. We use the "this.php_js" global (or object property in the namespaced version) to hold this information. See next(), prev(), etc. for some other functions which keep track of the array's pointer.

@Kevin, what will it take to get the site to update to reflect the latest git updates? It seems this fix was made a month ago...Thanks...

Gravatar
Itsacon
Jan 21st Permalink

q  The current version fails hard in Internet Explorer (both versions 7 and 8)

indexOf() is a method of the String object, not of the Array object, so calling it on pointers (an Array()) isn't valid javascript code.

I've reverted to the older version, which was less convoluted and worked fine:

function end(array)
{
	var last_elm,key;
	if(array.constructor==Array)
	{
		last_elm=array[(array.length-1)];
	}
	else
	{
		for(key in array)
		{
			last_elm=array[key];
		}
	}
	return last_elm;
}

Gravatar
Kevin van Zonneveld
16 Jun '08 Permalink

q  @ J A R: Much better, thank you!

Gravatar
J A R
15 Jun '08 Permalink

q  Doesn't .pop() return the last element from an array?
[CODE=&quot;Javascript&quot;]
function end ( array ) {
return array.pop();
}
[/CODE]

Gravatar
Kevin van Zonneveld
9 Jan '08 Permalink

q  @ Legaev Andrey: I sure did ;)

Gravatar
Legaev Andrey
9 Jan '08 Permalink

q  Did you mean &quot;return last_elm&quot; ?


Contribute a New function