phpjs
Version:
109 lines (92 loc) • 3.45 kB
Markdown
layout: page
title: "JavaScript array_change_key_case function"
comments: true
sharing: true
footer: true
alias:
- /functions/view/array_change_key_case:305
- /functions/view/array_change_key_case
- /functions/view/305
- /functions/array_change_key_case:305
- /functions/305
<!-- Generated by Rakefile:build -->
A JavaScript equivalent of PHP's array_change_key_case
{% codeblock array/array_change_key_case.js lang:js https://raw.github.com/kvz/phpjs/master/functions/array/array_change_key_case.js raw on github %}
function array_change_key_case (array, cs) {
// From: http://phpjs.org/functions
// + original by: Ates Goral (http://magnetiq.com)
// + improved by: marrtins
// + improved by: Brett Zamir (http://brett-zamir.me)
// * example 1: array_change_key_case(42);
// * returns 1: false
// * example 2: array_change_key_case([ 3, 5 ]);
// * returns 2: [3, 5]
// * example 3: array_change_key_case({ FuBaR: 42 });
// * returns 3: {"fubar": 42}
// * example 4: array_change_key_case({ FuBaR: 42 }, 'CASE_LOWER');
// * returns 4: {"fubar": 42}
// * example 5: array_change_key_case({ FuBaR: 42 }, 'CASE_UPPER');
// * returns 5: {"FUBAR": 42}
// * example 6: array_change_key_case({ FuBaR: 42 }, 2);
// * returns 6: {"FUBAR": 42}
// * example 7: ini_set('phpjs.return_phpjs_arrays', 'on');
// * example 7: var arr = [{a: 0}, {B: 1}, {c: 2}];
// * example 7: var newArr = array_change_key_case(arr);
// * example 7: newArr.splice(1, 1);
// * returns 7: {b: 1}
var case_fn, key, tmp_ar = {};
if (Object.prototype.toString.call(array) === '[object Array]') {
return array;
}
if (array && typeof array === 'object' && array.change_key_case) { // Duck-type check for our own array()-created PHPJS_Array
return array.change_key_case(cs);
}
if (array && typeof array === 'object' ) {
case_fn = (!cs || cs === 'CASE_LOWER') ? 'toLowerCase' : 'toUpperCase';
for (key in array) {
tmp_ar[key[case_fn]()] = array[key];
}
return tmp_ar;
}
return false;
}
{% endcodeblock %}
- [Raw function on GitHub](https://github.com/kvz/phpjs/blob/master/functions/array/array_change_key_case.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_change_key_case.js)
### Example 1
This code
{% codeblock lang:js example %}
array_change_key_case(42);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
false
{% endcodeblock %}
### Example 2
This code
{% codeblock lang:js example %}
array_change_key_case([ 3, 5 ]);
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
[3, 5]
{% endcodeblock %}
### Example 3
This code
{% codeblock lang:js example %}
array_change_key_case({ FuBaR: 42 });
{% endcodeblock %}
Should return
{% codeblock lang:js returns %}
{"fubar": 42}
{% endcodeblock %}
### Other PHP functions in the array extension
{% render_partial _includes/custom/array.html %}