gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 00:37:22 26 4
gpt4 key购买 nike

我试图检查一个状态是否包含在状态字符串数组中,但由于某种原因它总是等于 false。我这样写对吗?

我尝试过使用下划线包含,但它做了同样的事情

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);
};
})
})
})

例如,我应该对其中一些返回 true,而对其他返回 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

我对所有这些都感到错误。

最佳答案

data.location.split(', ').pop() 返回的值调用 JSON.stringify 意味着您正在与带引号的文本进行比较。

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

删除对 JSON.stringify 的调用

关于javascript - 为什么当我检查循环中的字符串是否包含在字符串数组中时,所有字符串都为 false?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58001322/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com