Use PHP functions in JavaScript

JavaScript array_reduce

Iteratively reduce the array to a single value via the callback.

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
2021
22
23
24
2526
27
28
function array_reduce (a_input, callback) {
    // Iteratively reduce the array to a single value via the callback.  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/array_reduce    // +   original by: Alfonso Jimenez (http://www.alfonsojimenez.com)
    // %        note 1: Takes a function as an argument, not a function's name
    // *     example 1: array_reduce([1, 2, 3, 4, 5], function (v, w){v += w;return v;});
    // *     returns 1: 15
        var lon = a_input.length;
    var res = 0, i = 0;
    var tmp = [];
 
        for (i = 0; i < lon; i+=2) {
        tmp[0] = a_input[i];
        if (a_input[(i+1)]) {
            tmp[1] = a_input[(i+1)];
        } else {            tmp[1] = 0;
        }
        res+= callback.apply(null, tmp);
        tmp = [];
    }    
    return res;
}
external links: original PHP docs | raw js source

Examples

Running

1
array_reduce([1, 2, 3, 4, 5], function (v, w){v += w;return v;});

Should return

1
15

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_reduce 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
Kevin van Zonneveld
5 Mar '08 Permalink

q  @ Alfonso Jiménez: Oh I should have seen that.. Anyway, I've updated the function and the test page gives the correct results now:
http://kevin.vanzonneveld.net/pj_tester.php

thanks again!

Gravatar
Alfonso Jiménez
5 Mar '08 Permalink

q  Arggg I made an error in the last example! It's &quot;is&quot; instead of &quot;mi&quot; :) If you realize &quot;mi&quot; is not in the string &quot;This is a Simple text&quot;.

I'll try to develop another contribution soon :)

Regards!

Gravatar
Kevin van Zonneveld
5 Mar '08 Permalink

q  @ Alfonso Jiménez: Hi Alfonso, thanks for your second contribution &amp; the example! Only one thing, the example produces: false instead of: 'is is a Simple text.'
Maybe you can see what's going wrong?
(btw, I had to add the '{' &amp; '}' to make it compatible with the packer)

Gravatar
Alfonso Jiménez
5 Mar '08 Permalink

q  BTW, Usage example:

[CODE=&quot;Javascript&quot;]strpbrk('This is a Simple text', 'mi')[/CODE]

Regards! Alfonso Jiménez (http://www.alfonsojimenez.com)

Gravatar
Alfonso Jiménez
5 Mar '08 Permalink

q  Hey Kevin. My second contribution is here :)

[CODE=&quot;Javascript&quot;]
function strpbrk(haystack, char_list) {
var lon = haystack.length;
var lon_search = char_list.length;
var ret = false;
var stack = '';

if(lon &gt;= lon_search) {
if(lon == lon_search) {
if(haystack == char_list)
ret = haystack;
} else {
j = 0;
i = 0;
while(i &lt; lon_search &amp;&amp; j &lt; lon &amp;&amp; !ret) {
if(char_list[i] == haystack[j]) {
i++;
if(i == lon_search) ret = true;
}
j++;
}

if(ret)
for(i = (j-lon_search); i &lt; lon; i++)
stack += haystack[i];

if(stack != '')
ret = stack;
}
}

return ret;
}[/CODE]

Re


Contribute a New function