简体   繁体   中英

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. 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.

JSON.stringify("Hello") -> "\"Hello\""

Remove the call to JSON.stringify

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2023 STACKOOM.COM