View: array_fill

Function:
Description: Create an array containing num elements starting with index start_key each initialized to val.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function array_fill (start_index, num, mixed_val) {
    // http://jsphp.co/jsphp/fn/view/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;
}
Comment on array_fill version 11
Tests
Benchmark
[top]

Comments

There are no comments yet, be the first!

Please Login or Register to post comments.