循环时是否可以仅提取关键部分?

时间:2016-07-29 14:05:08

标签: javascript

我有上面的键值对结构

循环时可以只提取密钥

$lReturn['html'] = $this->renderView(':App:Command/deliveryRange.html.twig');
$lReturn['deliveryRange'] = $deliveryRange;

return new Response(json_encode($lReturn), 200, array('Content-Type'=>'application/json'));

这样输出看起来像

var list_of_stocks= {
  "CBB": "Communications",
  "VZ": "Communications",
  "UPS": "Transportation"
};
for (var key in list_of_stocks) {
console.log(list_of_stocks[0]);
}

http://jsfiddle.net/n3fmw1mw/207/

1 个答案:

答案 0 :(得分:3)

只需记录key



var list_of_stocks= {
      "CBB": "Communications",
      "VZ": "Communications",
      "UPS": "Transportation",
      "RRTS": "Transportation",
      "AAC": "Heath"
};
for (var key in list_of_stocks) {
  console.log(key);
}




想要一系列钥匙?使用Object.keys()



var list_of_stocks= {
  "CBB": "Communications",
  "VZ": "Communications",
  "UPS": "Transportation",
  "RRTS": "Transportation",
  "AAC": "Heath"
};

var keys = Object.keys(list_of_stocks);

console.log(keys);
console.log(keys[0]); //CBB