在 PHP 中,我们可以遍历关联数组,并获取键和值的值,如下所示:
$myArray = array(
'key1' => 'value1',
'key2' => 'value2'
);
foreach($myArray as $key => $val){
echo 'The value of "'.$key.'" is "'.$val.'".\n';
}
/* Output:
The value of "key1" is "value1".
The value of "key2" is "value2".
*/
有什么办法可以在javascript中完成吗?
myObject = {
'key1': 'value1',
'key2': 'value2'
};
for (val in myObject) {
// check hasOwnProperty and what not...
// Now, how do I get the key value?
}