【发布时间】:2018-12-02 03:02:27
【问题描述】:
我正在尝试为一个变量启动一个 foreach,该变量可以是单个 id 或多个 id 的数组。首先,我尝试验证它是否是一个数组,如果不是,我将它声明为一个只有 1 项的数组。
router.post("/providerQuote", function (req, res) {
console.log(req.body.idQuote);
var idQuote = [];
if(req.body.idQuote.isArray)
{
idQuote = Object.values(req.body.idQuote);
}else{
idQuote = [req.body.idQuote];
}
console.log(idQuote);
idQuote.forEach(function (quote){
console.log(quote);
});
这是控制台日志:
Server Started...
[ '5bfed54c9b0d061574d874c0', '5bfed54c9b0d061574d874bf' ]
[ [ '5bfed54c9b0d061574d874c0', '5bfed54c9b0d061574d874bf' ] ]
[ '5bfed54c9b0d061574d874c0', '5bfed54c9b0d061574d874bf' ]
这里的问题是它以某种方式将 req.body 插入到另一个数组中。
【问题讨论】:
-
你的意思是使用
if (Array.isArray(req.body.idQuote))?
标签: javascript arrays node.js