2014-09-04 50 views
0

以下5行可以壓縮爲單行數組嗎?Jquery數組 - 如何將多個索引初始化壓縮爲單行代碼

var exmplArray = []; 
exmplArray['hours'] = 0; 
exmplArray['overtime'] = 0; 
exmplArray['income'] = 0; 
exmplArray['expenditure'] = 0; 

我曾嘗試以下,但在錯誤踢:「未捕獲的ReferenceError:在分配無效左側」

var exmplArray = ['hours' = 0, 'overtime' = 0, 'income' = 0, 'expenditure' = 0]; 

什麼想法?

回答

1

您應該使用對象{},而不是當你需要在JavaScript一個 「哈希」:

var example = {}; 
example['hours'] = 0; 
example['overtime'] = 0; 
example['income'] = 0; 
example['expenditure'] = 0; 

// the quote could be ommitted. 
var example = { 'hours': 0, 'overtime': 0, 'income': 0, 'expenditure': 0 }; 
0

嘗試

var exmplArray = {'hours' :0, 'overtime' :0, 'income' :0, 'expenditure' :0};