下面的5行可以压缩成一个单行数组吗?
var exmplArray = [];
exmplArray['hours'] = 0;
exmplArray['overtime'] = 0;
exmplArray['income'] = 0;
exmplArray['expenditure'] = 0;
我已尝试以下操作,但出现错误:“ Uncaught ReferenceError :赋值中的左侧无效”
var exmplArray = ['hours' = 0, 'overtime' = 0, 'income' = 0, 'expenditure' = 0];
有什么想法吗?
最佳答案
当您需要 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 };
关于javascript - Jquery Array - 如何将多个索引初始化压缩为一行代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25660781/