Jquery数组 - 如何将多个索引初始化压缩为单行代码

时间:2014-09-04 08:32:38

标签: javascript jquery arrays

可以将以下5行压缩成单行数组吗?

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];

任何想法?

2 个答案:

答案 0 :(得分:1)

当你需要在javascript中使用“hash”时,你应该使用对象{}

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 };

答案 1 :(得分:0)

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