可以将以下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];
任何想法?
答案 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};