简体   繁体   English

为什么当我检查循环中的字符串是否包含在字符串数组中时,所有字符串都为假?

[英]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.

相关问题 我如何让网络驱动程序查看包含的html文件中的元素 - How do i get webdriver to see elements in included html files 尝试从数组中打印字符串时,为什么会出现EJS语法错误? - Why do I get an EJS syntax error when trying to print strings from an array? 当一个对象的键是长字符串时,为什么我看不到它的所有键和值? - Why I don't see all the keys and values of an object when its keys are long strings? 如何删除所有其他字符串,但在数组中删除“metal”中的字符串 - How do I remove every other string but strings with “metal” in them in an array Angular.js-如何使用过滤器检查数组对象中是否包含字符串? - Angular.js - How do I check to see if a string is contained within an array object using a filter? 如何将字符串与字符串数组匹配 - How do I match string with the array of strings 如何获取字符串数组并过滤它们? - How do I take an array of strings and filter them? 为什么尝试使用Stringify将数组转换为JSON字符串时出现方括号 - Why do I get square brackets when trying to convert array to JSON string using stringify 为什么添加带有for循环的事件列表器时为什么会出现TypeError? - Why do I get a TypeError when adding event listners with a for loop? 我传递给它的false语句引用字符串时,while循环没有停止 - while loop not stopping when I pass it a false statement referencing a string
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM