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; } |
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:
[CODE="Javascript"]$keys = {'a':'foo', 2:5, 3:10, 4:'bar'};
//$keys = ['foo', 5, 10, 'bar'];[/CODE]
[CODE="Javascript"]function array_fill_keys (keys, value) {
var retObj={};
for (var key in keys) {
retObj[keys[key]] = value;
}
return retObj;
}[/CODE]
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');
[CODE="Javascript"]
function array_fill_keys (keys, value) {
for (var i=0, retObj={}; i < keys.length; i++) {
retObj[keys[i]] = value;
}
return retObj;
}[/CODE]
@ _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 :
[CODE="Javascript"]
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 );
}
[/CODE]


Kevin van Zonneveld
25 Nov '08