Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

How to get all the suggested string on the basis of search string in javascript

I have two string, one which contains all the values of countries and second string which I entered when I’m searching for country. Now, I want all the results which contains "In" like result will be
India, Indonesia

as I have search for IN

// s1="In", s2="countries value"

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

const s2 = "India, Australia,Canada,Luxembourg,Singapore,Switzerland,United States of America, Indonesia";
const s1 = "In"

const findString = (s1, s2) => {
if(s2.includes(s1.charAt(0).toUpperCase() + s1.slice(1))){
  return s1;
} else {
  return null;
}

}

this function returning me "IN" but I want "India, Indonesia" as result

>Solution :

const s2 = "India, Australia,Canada,Luxembourg,Singapore,Switzerland,United States of America, Indonesia";
const s2Arr = s2.replace(/, +/g, ',').split(',') // create array all country

function findString(keyword) {
  keyword = keyword.toLowerCase()

  return s2Arr.filter(item => item.toLowerCase().includes(keyword))
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading