Use PHP functions in JavaScript

JavaScript array_fill

Create an array containing num elements starting with index start_key each initialized to val

1
2
3
4
56
7
8
9
1011
12
13
14
1516
17
18
19
function array_fill (start_index, num, mixed_val) {
    // Create an array containing num elements starting with index start_key each initialized to val  
    // 
    // version: 1008.1718
    // discuss at: http://phpjs.org/functions/array_fill    // +   original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net)
    // +   improved by: Waldo Malqui Silva
    // *     example 1: array_fill(5, 6, 'banana');
    // *     returns 1: { 5: 'banana', 6: 'banana', 7: 'banana', 8: 'banana', 9: 'banana', 10: 'banana' }
    var key, tmp_arr = {}; 
    if ( !isNaN( start_index ) && !isNaN( num ) ) {
        for (key = 0; key < num; key++) {
            tmp_arr[(key+start_index)] = mixed_val;
        }    }
 
    return tmp_arr;
}
external links: original PHP docs | raw js source

Examples

Running

1
array_fill(5, 6, 'banana');

Should return

1
{ 5: 'banana', 6: 'banana', 7: 'banana', 8: 'banana', 9: 'banana', 10: 'banana' }

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_fill 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
25 Nov '08 Permalink

q  @ Brett Zamir: Cool, thx!

Gravatar
Brett Zamir
21 Nov '08 Permalink

q  Yeah, you were right, sorry. Here it is to accept associative as well as regular arrays for 'keys':

Examples:
[CODE=&quot;Javascript&quot;]$keys = {'a':'foo', 2:5, 3:10, 4:'bar'};
//$keys = ['foo', 5, 10, 'bar'];[/CODE]


[CODE=&quot;Javascript&quot;]function array_fill_keys (keys, value) {
var retObj={};
for (var key in keys) {
retObj[keys[key]] = value;
}
return retObj;
}[/CODE]

Sorry for the trouble...

Gravatar
Kevin van Zonneveld
18 Nov '08 Permalink

q  @ Brett Zamir: Cool, I've committed it. Will be online shortly. Are you sure by the way, that PHP does not allow associative arrays as 'keys'? Otherwise we will have to change the for-loop to support this.

Gravatar
Brett Zamir
16 Nov '08 Permalink

q  Here's one for array_fill_keys() with a sample based on the PHP manual page for array_fill_keys():

$keys = ['foo', 5, 10, 'bar'];
$a = array_fill_keys($keys, 'banana');

[CODE=&quot;Javascript&quot;]
function array_fill_keys (keys, value) {
for (var i=0, retObj={}; i &lt; keys.length; i++) {
retObj[keys[i]] = value;
}
return retObj;
}[/CODE]

Gravatar
Kevin van Zonneveld
29 Jan '08 Permalink

q  @ _argos: Hey argos, Thanks for your input, I've added everything to the project. If you know any more, feel free to leave another comment any time :)

Gravatar
waldo malqui silva aka _argos
29 Jan '08 Permalink

q  Hi Kevin.

I have some ports to your project, and I wanna know if you change my real name by _argos, sorry for duplicate one function ( array_fill ) and make one function (array_pad) on that you are working :

[CODE=&quot;Javascript&quot;]
var Test1 = 4.2;

var Test2 = -4.2;

var Test3 = 5;

var Test4 = -5;

var Test5 = 'prueba';

console.log ( abs ( Test1 ) );

console.log ( abs ( Test2 ) );

console.log ( abs ( Test3 ) );

console.log ( abs ( Test4 ) );

console.log ( abs ( Test5 ) );


function abs ( mixed_number ) {

return ( ( !isNaN ( mixed_number ) ) ? ( ( mixed_number &lt; 0 ) ? ( mixed_number * -1 ) : mixed_number ) : 0 );

}



var TestA = [ 7,8,9 ];



console.log ( array_pad ( TestA, 2, 'a' ) );

console.log ( array_pad ( TestA, 5, 'a' ) );

console.log ( array_pad ( TestA, 5, 2 ) );

console.log ( array_pad ( TestA, -5, 'a' ) );

console.log ( array_pad ( TestA, -5, 2 ) );



function array_pad ( input, pad_size, pad_value ) {

var pad = [];

if ( input instanceof Array &amp;&amp; !isNaN ( pad_size ) ) {
var newArray = [];
var newLength = ( ( pad_size &lt; 0 ) ? ( pad_size * -1 ) : pad_size );

if ( newLength &gt; input.length ) {
for ( var i = 0; i &lt; ( newLength - input.length ); i++ ) { newArray [ i ] = pad_value; }

pad = ( ( pad_size &lt; 0 ) ? newArray.concat ( input ) : input.concat ( newArray ) );

} else {

pad = input;

}

}

return pad;

}



function array_fill ( start_key, num, val ) {

var fill = [];



if ( !isNaN ( start_key ) &amp;&amp; !isNaN ( num ) ) {

for ( var i = start_key; i &lt; (start_key + num ); i++ ) {

fill [ i ] = val;

}

}



return fill;

}



console.log ( strlen ( 'alexa' ) );


function strlen ( str ) {

return str.length;

}



console.log ( implode ( '-', [1,2,3,4,5,6,7,8,9,0] ) );



function implode ( glue, pieces ) {

return ( ( pieces instanceof Array ) ? pieces.join ( glue ) : pieces );

}
[/CODE]


Contribute a New function