-3

在下面的代码中 question1 的值是NaN. 我想将其转换为 0,但我无法使用parseInt. 我怎样才能做到这一点?

app.post('/auth/total',function(req,res){
    console.log(req.body);
    const {  question1, question2, question3, question4} = req.body;
    let total1 = parseInt(question1) + parseInt(question2) 
    let total2 = parseInt(question3) + parseInt(question4) 
    
    //some code to insert data in db and render the view 

});
4

1 回答 1

1

你可以这样做:

let total1 = (parseInt(question1) || 0) + (parseInt(question2) || 0)

这会将任何虚假值转换为 0

于 2022-02-12T17:35:48.070 回答