我在JavaScript的新的,這裏是一個新手的問題:提取JSON轉換爲JavaScript變量
在PHP中,有一個extract()方法,可以從一個關聯數組變量導入。例如:
$var_array = array("color" => "blue",
"size" => "medium",
"shape" => "sphere");
extract($var_array);
// $color is a defined varible here with the value of "blue"
我不知道是否有標準JavaScript的方法,或在流行的庫如jQuery或下劃線,做同樣的JavaScript對象。我的意思是這樣的:
jsObject = {"color" : "blue",
"size" : "medium",
"shape" : "sphere"};
extract(jsObject); // Is there such a thing?
// Here, color is a defined variable
沒有沒有這樣的功能,但你爲什麼需要這個? –
你爲什麼需要它?您可以僅使用JSON並使用JSON.parse()將其作爲對象進行轉換。您可以使用myObject.color – rakwaht
輕鬆訪問顏色值,您可以聲明爲'window'對象(全局作用域):'jsObject.forEach(function(value,key){window [key] = value;});' – ramabarca