phpjs
Version:
64 lines (51 loc) • 2.04 kB
Markdown
layout: page
title: "JavaScript array_fill_keys function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/array_fill_keys:315
- /functions/view/array_fill_keys
- /functions/view/315
- /functions/array_fill_keys:315
- /functions/315
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's array_fill_keys
{% codeblock array/array_fill_keys.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_fill_keys.js raw on github %}
function array_fill_keys (keys, value) {
// From: http://phpjs.org/functions
// + 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;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_fill_keys.js)
Please note that php.js uses JavaScript objects as substitutes for PHP arrays, they are
the closest match to this hashtable-like data structure.
Please also note that php.js offers community built functions and goes by the
[McDonald's Theory](https://medium.com/what-i-learned-building/9216e1c9da7d). We'll put online
functions that are far from perfect, in the hopes to spark better contributions.
Do you have one? Then please just:
- [Edit on GitHub](https://github.com/kvz/phpjs/edit/master/functions/array/array_fill_keys.js)
### Example 1
This code
{% codeblock lang:js example %}
keys = {'a': 'foo', 2: 5, 3: 10, 4: 'bar'}
array_fill_keys(keys, 'banana')
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{"foo": "banana", 5: "banana", 10: "banana", "bar": "banana"}
{% endcodeblock %}
### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}