[英]Why when I check to see if a string in a loop is included in an array of strings do I get false for all of them?
I'm trying to check if a state is included in an array of state strings, but for some reason it's always equating to false.我正在尝试检查 state 是否包含在 state 字符串数组中,但由于某种原因,它总是等于 false。 Am I writing this right?我这样写对吗?
I've tried using underscore contains, but it does the same thing我试过使用下划线包含,但它做同样的事情
T.get('followers/list', {screen_name: ''}, function(err, data, response){
let states = [
"AK",
"AL",
"AR",
"AS",
"AZ",
"CA",
"CO",
"CT",
"DC",
"DE",
"FL",
"GA",
"GU",
"HI",
"IA",
"ID",
"IL",
"IN",
"KS",
"KY",
"LA",
"MA",
"MD",
"ME",
"MI",
"MN",
"MO",
"MS",
"MT",
"NC",
"ND",
"NE",
"NH",
"NJ",
"NM",
"NV",
"NY",
"OH",
"OK",
"OR",
"PA",
"PR",
"RI",
"SC",
"SD",
"TN",
"TX",
"UT",
"VA",
"VI",
"VT",
"WA",
"WI",
"WV",
"WY"
]
_.each(data, loc => {
_.each(loc, data => {
if(data.location){
var statesyes = states.includes(JSON.stringify(data.location.split(', ').pop()));
console.log(statesyes);
};
})
})
})
For instance, I should be getting back true for some of these and false for others, which is the goal.例如,我应该对其中一些恢复为真,对另一些恢复为假,这就是目标。 But I'm only getting false.但我只会变得虚假。
false
"South Africa"
false
"South Africa"
false
"The dark part of the web"
false
"MI"
false
"FL"
false
"United States"
false
"WV"
false
"TX"
false
"Iowa"
false
"IA"
false
"MN"
false
"IA"
false
"Big Sky Country"
false
"USA"
false
"MO"
false
"IA"
false
I'm getting false for all of them.我对他们所有人都是假的。
Invoking JSON.stringify
on the value returned from data.location.split(', ').pop()
means that you're comparing with quoted text.对从data.location.split(', ').pop()
返回的值调用JSON.stringify
意味着您正在与引用的文本进行比较。
JSON.stringify("Hello")
-> "\"Hello\""
JSON.stringify("Hello")
-> "\"Hello\""
Remove the call to JSON.stringify
删除对JSON.stringify
的调用
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.