我有一个函数,用underscore
映射数组。以下是该函数:
var services = _.map(userCopy.get('services'), function (service) {
if (service.service_selected === true) {
return service;
}
});
我的问题是当条件conditional === false
时,services
会接收到undefined
。当然我可以去掉undefined
,但那很丑陋,我只想正确使用map
。我该如何解决这个问题?
conditional === false
,JavaScript 解释器将不会找到任何结构并转到隐式返回语句,该语句返回undefined
- http://shamansir.github.io/JavaScript-Garden/en/#core.undefined - 要返回false
,必须显式地这样做。 - Andrei Shostik