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: 909.322 // 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; } |
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.
Yeah, you were right, sorry. Here it is to accept associative as well as regular arrays for 'keys':
Examples:
1 2 | $keys = {'a':'foo', 2:5, 3:10, 4:'bar'}; //$keys = ['foo', 5, 10, 'bar']; |
1
2
3
4
56
7
| function array_fill_keys (keys, value) { var retObj={}; for (var key in keys) { retObj[keys[key]] = value; } return retObj; } |
Sorry for the trouble...
@ 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.
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');
1
2
3
4
56
| function array_fill_keys (keys, value) { for (var i=0, retObj={}; i < keys.length; i++) { retObj[keys[i]] = value; } return retObj;} |
@ _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 :)
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 :
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 5556 57 58 59 6061 62 63 64 6566 67 68 69 7071 72 73 74 7576 77 78 79 8081 82 83 84 8586 87 88 89 9091 92 93 94 9596 97 98 99 100101 102 103 104 105106 107 108 109 110111 112 113 114 115116 | 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 < 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 && !isNaN ( pad_size ) ) { var newArray = []; var newLength = ( ( pad_size < 0 ) ? ( pad_size * -1 ) : pad_size ); if ( newLength > input.length ) { for ( var i = 0; i < ( newLength - input.length ); i++ ) { newArray [ i ] = pad_value; } pad = ( ( pad_size < 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 ) && !isNaN ( num ) ) { for ( var i = start_key; i < (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 ); } |


Kevin van Zonneveld
25 Nov '08