[英]How to loop through an array to generate js code?
Is there a way to loop through an array (or something similar) when generating js code?生成js代码时有没有办法遍历数组(或类似的东西)? For example, I have this in mongoose:例如,我在 mongoose 中有这个:
users.updateOne({'_id': req.user._id}
, {
["local.minMinutes"]: req.body.minMinutes
, ["local.color1U"]: req.body.color1U
, ["local.color2U"]: req.body.color2U
, ["local.color3U"]: req.body.color3U
, ["local.color4U"]: req.body.color4U
, ["local.color5U"]: req.body.color5U
});
I'd like to be able to say "loop through 1 to 5" instead of having to write each individual number out, but a for() loop inside the mongoose call is giving all sorts of error.我希望能够说“循环 1 到 5”,而不必写出每个单独的数字,但是 mongoose 调用中的 for() 循环会产生各种错误。 I hope this question is specific enough - thank you!我希望这个问题足够具体-谢谢!
Use a for
loop to create the object by copying the properties from req.body
.使用for
循环通过从 req.body 复制属性来创建req.body
。
obj = {
};
for (prop in req.body) {
obj[`local.${prop}`] = req.body[prop];
}
users.updateOne({'_id': req.user._id}, obj);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.