需要帮助使用 array.some() 检查数组中的现有城市是否包含在短语数组中

Need help to use array.some() to check if existing cities in arrays are included in array of phrases

我有一个城市列表和一个短语列表。我需要检查短语中是否存在城市,并基于此 return“大城市”或“不是大城市”

我认为使用 some() 函数会很简单,但我似乎不清楚它是如何工作的。我试过用一个字符串表示一个城市和一系列城市..但两者似乎都不起作用..

我已经检查了这个问题 (),也许答案在那里,但似乎在那里使用了过滤器..我必须这样做还是我不能只使用一些然后得到结果?

如果能解释一下为什么下面的代码不起作用,我将不胜感激。我花了一天的时间试图弄明白,但我似乎做不到。在我的脑海中 result 在下面的代码中应该是 'big city' 两次.. 一次是 'new york' 一次是 'milan'

// COMMON FOR BOTH

const phrases = ['I live in San Francisco','I live in milan in italy', 'I live in Los Angeles', 'I live in New York City']


// WITH AN ARRAY

const cities=['paris',"new york",'milan'];

function checkCities(phrase){
  
  cities.forEach(city=> phrase.includes(city));

};
                       
const result1 = phrases.some(checkCities) ? "big city" : "not a big city";


console.log('result1',result1); // Output "not a big city"


// WITH JUST A STRING

const city = 'new york';

const result2 = phrases.some(checkCity) ? "big city" : "not a big city";

console.log('result2',result2); // Output "not a big city

function checkCity(phrase){
  
  phrase.includes(city);

};



...
// WITH JUST A STRING
...
function checkCity(phrase){
  if (phrase.toLowerCase().includes(city.toLowerCase())) return phrase
};

使用数组:您可以使用“JUST STRING”代码循环遍历每个城市

一个建议:因为 includes 区分大小写。因此,'new york'.includes('New York') 将 return false。您可以转换 lowerCase() 中的两个字符串以检查它是否存在。

工作演示:

const phrases = ['I live in San Francisco','I live in milan in italy', 'I live in Los Angeles', 'I live in New York City'];

const city = 'new york';

function checkCities(city) {
  return phrases.some((phrase) => phrase.toLowerCase().indexOf(city.toLowerCase()) > -1);
}

console.log(checkCities(city) ? "big city" : "not a big city");

注意:如果您想一次搜索一个城市,以上代码将完美运行。

您的问题是您误解了传递给 .some() 的函数应该如何工作。您传递给 .some() 的函数将被 JS 多次调用,对于您调用 .some() 方法的数组中的每个元素一次 - 在您的情况下,即 [=14 中的每个字符串=]数组。该函数还应该 return 一个 truefalse 值(或一个 truthy/falsy value). When you return true from your function, the .some() method stops any further iterations and returns true (indicating you've met the condition you're trying to check), if you return false, the .some() method will continue its loop and check the next element in your array. If the function passed to .some() doesn't return true for any value in your array, then the .some() method returns false after checking all items in your array. You can read abut the details of the .some() method on MDN.

在您的代码中,您传递给 .some()checkCities 的函数不会 return 任何内容,因此它自动 return 的值为undefined 每次 JS 调用它时,.some() 方法与每个元素 returning false 相同。

如您所述,您想要“检查是否在任何 (.some()) 个短语中,如果存在任何 (.some()) 个城市,则 return “一个大城市””。我们可以试着把这句话变成代码(见下面的代码注释):

const phrases = ['I live in San Francisco','I live in milan in italy', 'I live in Los Angeles', 'I live in New York City'];
const cities = ['paris',"new york",'milan'];

// If "any"--------------v   of the phrases...                   
const result1 = phrases.some(checkCities) ? "big city" : "not a big city";

function checkCities(phrase){
  // If "any"---v of the cities "exist"------------v
  return cities.some(city => phrase.toLowerCase().includes(city.toLowerCase()));
  //                      ^--- arrow function without body `{}`, so the return value is implicit
};

console.log('result1', result1); // Output "not a big city"

这里我们使用了两个 .some() 方法,一个在外部 phrases 来遍历每个 phrase,另一个在内部 .some() 来检查是否有city 个字符串在当前 phrase 中。如果内.some()方法returns true(说明在phrase中找到了city),那么你的外.some()方法还将 return true,否则,如果内部 .some() 方法无法在任何短语中找到城市,则它将 return false 用于每个 phrase 由外部循环迭代,这将导致您的外部 .some() 方法 returning false.