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

Time:03-14

I have a list of cities and I have a list of phrases. I need to check if the cities exist in the phrases and based on this return "big city" or "not a big city"

I thought it would be simple with the some() function but I seem to be not clear about how it really works. I've tried with with a single string for a city and an array of cities.. but both don't seem to work..

I have check this question (Checking if elements of an array are included in elements of another array) and maybe the answer is there but there seems to be a use of filter there.. do I have to do that or can I not just use some and get the result?

I'd appreciate some help with some explanation why the below code does not work.. I've lost one day trying to figure it out but I can't seem to.. In my head the result should be 'big city' twice in the code below .. once for 'new york' and once for '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);

};



CodePudding user response:

Your issue is with your misunderstanding of how the function you pass to .some() should work. The function you pass to .some() will be called by JS multiple times, once for each element in the array that you called the .some() method on - in your case, that is each string in the phrases array. The function should also return a true or false value (or a 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.

In your code, the function you're passing to .some(), checkCities, does not return anything from it, so it automatically returns a value of undefined every time JS calls it, which to the .some() method is the same as returning false for each element.

As you outlined, you want to "check if, in any (.some()) of the phrases, if any (.some()) of the cities exist then return a "a big city"". We can try and turn this sentence into code (see code comments below):

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"

Here we're using two .some() methods, one on the outer phrases to iterate through each phrase, and the an inner .some() to check if any of the city strings are in the current phrase. If the inner .some() method returns true (indicating that a city has been found in a phrase), then your outer .some() method will also return true, otherwise, if the inner .some() method is unable to find a city in any phrase, then it will return false for each phrase that is iterated on by the outer loop, which will result in your outer .some() method returning false.

CodePudding user response:

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

WITH AN ARRAY: you can loop for each city with "JUST STRING" code

CodePudding user response:

One suggestion : As includes is a case sensitive. Hence, 'new york'.includes('New York') will return false. You can either convert both the strings in lowerCase() to check if it exist or not.

Working Demo :

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

Note : Above code will work perfectly if you want to search for a single city at a time.

  • Related