PHP.jsライブラリarray_keys
からの良い例を次に示します。
function array_keys (input, search_value, argStrict) {
// Return just the keys from the input array, optionally only for the specified search_value
var search = typeof search_value !== 'undefined',
tmp_arr = [],
strict = !!argStrict,
include = true,
key = '';
for (key in input) {
if (input.hasOwnProperty(key)) {
include = true;
if (search) {
if (strict && input[key] !== search_value) {
include = false;
}
else if (input[key] != search_value) {
include = false;
}
}
if (include) {
tmp_arr[tmp_arr.length] = key;
}
}
}
return tmp_arr;
}
同じことがarray_values
(同じPHP.jsライブラリから)当てはまります:
function array_values (input) {
// Return just the values from the input array
var tmp_arr = [],
key = '';
for (key in input) {
tmp_arr[tmp_arr.length] = input[key];
}
return tmp_arr;
}