【发布时间】:2021-08-15 11:22:43
【问题描述】:
所以我有一个空数组,我想添加具有在运行时确定的值的对象。我想出了这段代码:
var res = []; //the result array which needs to be populated
var rn = Math.random() * 5;
var value1 = rn * 75,
value2 = rn * 27.91;
let galf = {
type: rn;
v1l: Math.floor(value1 / 7);
v2r: value2 % 7;
}
res.push(galf);
console.log(res)
现在看来,我对 galf 对象的声明是不稳定的。如果我注释掉这个声明,那么代码运行良好。
如果要在运行时确定对象的值,如何将对象正确添加到数组中?
【问题讨论】:
-
我给你做了一个sn-p。它抛出的错误非常有用。正如 LeeLenalee 所说
You shouldnt use ; in an object, to add more fields you need to use the , to separate fields
标签: javascript arrays javascript-objects