Reputation: 200
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"
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
Upvotes: 0
Views: 42
Reputation: 3908
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))
}
Upvotes: 1
Reputation: 1523
Try like this:
const s2 = "India, Australia,Canada,Luxembourg,Singapore,Switzerland,United States of America, Indonesia";
const s1 = "In"
const findString = (s1, s2) => s2.split(',').filter((s) => s.indexOf(s1) === 0);
Upvotes: 0