JavaScript array_keys
Return just the keys from the input array, optionally only for the specified search_value
1 2 3 4 56 7 8 9 1011 12 13 14 1516 17 18 19 2021 22 23 24 2526 27 28 29 3031 32 | function array_keys (input, search_value, argStrict) { // Return just the keys from the input array, optionally only for the specified search_value // // version: 1008.1718 // discuss at: http://phpjs.org/functions/array_keys // + original by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // + input by: Brett Zamir (http://brett-zamir.me) // + bugfixed by: Kevin van Zonneveld (http://kevin.vanzonneveld.net) // * example 1: array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} ); // * returns 1: {0: 'firstname', 1: 'surname'} var tmp_arr = {}, strict = !!argStrict, include = true, cnt = 0; var key = ''; for (key in input) { include = true; if (search_value != undefined) { if (strict && input[key] !== search_value){ include = false; } else if (input[key] != search_value){ include = false; } } if (include) { tmp_arr[cnt] = key; cnt++; } } return tmp_arr; } |
Examples
Running
1 | array_keys( {firstname: 'Kevin', surname: 'van Zonneveld'} ); |
Should return
1 | {0: 'firstname', 1: 'surname'} |
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_keys goodness in JavaScript.
Doesn't seem to work on Safari 4 ..
Neither on Safari 3.x (i'm actually developping for iPhone / iPad).
Hello Mickael,
This should definitely be working in Firefox, as that is what i use to test it. Can you please give example code which causes the problem for you? Are you using any other libraries with your code like Prototype?
Hi,
I test this function with Mozilla Firefox 3.5.7 but it does not work.
I think that Mozilla has a bug concerning "for in" syntax.
Thanks for all scripts, PHP.JS is a very good work :)
Hmmm. I should change that to:
[CODE="Javascript"]
const CASE_LOWER = 0;
const CASE_UPPER = 1;
[/CODE]
Hey Kevin, here's my implementation for array_change_key_case().
[CODE="Javascript"]
var CASE_LOWER = 0;
var CASE_UPPER = 1;
function array_change_key_case(array) {
// * example 1: array_change_key_case(42);
// * returns 1: false
// * example 2: array_change_key_case([ 3, 5 ]);
// * returns 2: {0: 3, 1: 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}
if (array instanceof Array) {
return array;
}
if (array instanceof Object) {
var case_fn = (arguments.length == 1 || arguments[1] == CASE_LOWER) ?
"toLowerCase" : "toUpperCase";
var ret = new Object();
for (var key in array) {
ret[key[case_fn]()] = array[key];
}
return ret;
}
return false;
}
[/CODE]


Brett Zamir
May 12th