[英]If Else and Javascript what am I missing?
I think there is an issue in my if else statement as I think it is giving me the opposite result of what I'm expecting. 我认为if else语句中存在一个问题,因为它给了我与预期相反的结果。
console.log(_.map(item, function(n){return _.find(n).tertiary}))
//is undefined.
console.log(_.map(item, function(n){return _.find(n).primary}));
//is ["green"]
I use the following if statement to assign a value to my variable:
if(_.map(item, function(n){return _.find(n).tertiary}) === undefined ){
var audience = _.map(item, function(n){return _.find(n).tertiary});
} else {
var audience = _.map(item, function (n) {return _.find(n).primary});
}
console.log(audience);//this is undefined
Am I doing something wrong or is this working correctly? 我是在做错事还是正常工作? I expect the answer to be ["green"].
我希望答案是[“绿色”]。
Change your if else to: 将您的if else更改为:
if(_.map(item, function(n){return _.find(n).tertiary}) === undefined ){//since tertiary is undefined, so use primary instead
var audience = _.map(item, function (n) {return _.find(n).primary});
} else {
var audience = _.map(item, function(n){return _.find(n).tertiary});
}
Just for a little variety: 只是为了一点点变化:
var colours = _.map(item, function(n){
return {primary: _.find(n).primary, tertiary: _.find(n).tertiary}
})
var audience = colours.primary || colours.tertiary
console.log(audience)
Although underscore/lodash could probably lend a hand in getting those values out in a cleaner manner. 尽管下划线/破折号可能会帮助以更清洁的方式体现这些价值观。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.