View: array_fill_keys

Function:
Description: Create an array using the elements of the first parameter as keys each initialized to val.
Code
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
function array_fill_keys (keys, value) {
    // http://jsphp.co/jsphp/fn/view/array_fill_keys
    // +   original by: Brett Zamir (http://brett-zamir.me)
    // +   bugfixed by: Brett Zamir (http://brett-zamir.me)
    // *     example 1: keys = {'a': 'foo', 2: 5, 3: 10, 4: 'bar'}
    // *     example 1: array_fill_keys(keys, 'banana')
    // *     returns 1: {"foo": "banana", 5: "banana", 10: "banana", "bar": "banana"}
    var retObj = {},
        key = '';

    for (key in keys) {
        retObj[keys[key]] = value;
    }

    return retObj;
}
Comment on array_fill_keys version 12
Tests
Benchmark
[top]

Comments

There are no comments yet, be the first!

Please Login or Register to post comments.