{"id":19768,"date":"2022-11-07T16:55:58","date_gmt":"2022-11-07T11:25:58","guid":{"rendered":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/"},"modified":"2022-11-07T16:55:58","modified_gmt":"2022-11-07T11:25:58","slug":"solved-find-cities-and-country-name-from-string","status":"publish","type":"post","link":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/","title":{"rendered":"[Solved] find cities and country name from string"},"content":{"rendered":"<p> [ad_1]<br \/>\n<\/p>\n<div id=\"answer-52506701\" class=\"answer js-answer accepted-answer js-accepted-answer\" data-answerid=\"52506701\" data-parentid=\"52490519\" data-score=\"0\" data-position-on-page=\"1\" data-highest-scored=\"1\" data-question-has-accepted-highest-score=\"1\" itemprop=\"acceptedAnswer\" itemscope itemtype=\"https:\/\/schema.org\/Answer\">\n<div class=\"post-layout\">\n<div class=\"votecell post-layout--left\"><\/div>\n<div class=\"answercell post-layout--right\">\n<div class=\"s-prose js-post-body\" itemprop=\"text\">\n<p>It all starts from <code>function start()<\/code> at the bottom. For displaying purpose i&#8217;ve used a small dataset but you can require the data from the json file by using <code>const data = require('data.json')<\/code>. I&#8217;ve tested for large dataset also, works like a charm. Hope it helps.<\/p>\n<\/p>\n<div class=\"snippet\" data-lang=\"js\" data-hide=\"false\" data-console=\"true\" data-babel=\"false\">\n<div class=\"snippet-code\">\n<pre class=\"snippet-code-js lang-js prettyprint-override\"><code>const data = {\r\n    \"United States\":[\r\n    \"Washington\",\"Bratislava\",\"Hard\",\"Going\"],  \r\n    \"Afghanistan\": [\r\n    \"Herat\",\r\n    \"Kabul\",\r\n    \"Kandahar\",\r\n    \"Molah\",\r\n    \"Rana\",\r\n    \"Shar\",\r\n    \"Sharif\",\r\n    \"Wazir Akbar Khan\"\r\n]};\r\nArray.prototype.myJoin = function(start,end){\r\n    if(!start) start = 0;\r\n    if(!end) end = this.length - 1;\r\n    end++;\r\n    return this.slice(start,end);\r\n};\r\n\r\n\r\nconst getCityData = async (country) =&gt; {\r\n    return country;\r\n}\r\n\r\nconst changeFormat = async () =&gt; {\r\n    try {\r\n        let countries = Object.keys(data).map( (country, index) =&gt; {\r\n            return country;\r\n        })\r\n        let citiesData = [];\r\n        await countries.map( (country, index) =&gt; {\r\n            citiesData = citiesData.concat(data[country]);\r\n        })\r\n        return countries.concat(citiesData);\r\n    } catch (err) {\r\n        return err;\r\n    }\r\n}\r\nconst checkSentence = (text, text_original, number, modified_data) =&gt; {\r\n    return new Promise((resolve, reject)=&gt;{\r\n        try {\r\n            if( !text || !text.length ){\r\n                throw new Error('empty text');\r\n            }\r\n            \/\/ console.log('started ' + number);\r\n            \/\/ console.log('number ' + number +' started')\r\n            let upperCase = [];\r\n            const number_const = number;\r\n            let temp1 = new Array(text.length);\r\n            temp1.fill(2);\r\n            temp1.map( (v, i) =&gt; {\r\n                \/\/ let temp = text;\r\n                let temp = [...text_original, ...[]];\r\n                \/\/ console.log('i' + i);\r\n                \/\/ console.log('number' + number);\r\n                \r\n                if(i + number &lt;= text.length ) {\r\n                    \/\/ console.log('inside 1st if');\r\n                    temp = temp.slice(i, i + number)\r\n                    \/\/ console.log(text + ' 1');\r\n                    temp = temp.join(' ')\r\n                    \/\/ console.log(text + ' 2');                    \r\n                    temp = temp.toLowerCase();\r\n                    \/\/ console.log(text + ' 3');                    \r\n                    if(modified_data.indexOf(temp) != -1){                        \r\n                        upperCase.push({ start: i, end: i + number - 1 })\r\n                    }\r\n                }\r\n            })\r\n            \r\n            let toBeChanged = [];\r\n            if(upperCase.length){\r\n             upperCase.map( (v, i) =&gt; {\r\n                 \/\/ console.log(v);\r\n                 let arr = range( v.start, v.end )\r\n                 toBeChanged = toBeChanged.concat(arr);\r\n             })\r\n            }\r\n            \/\/ console.log('ended number' + number);\r\n            \/\/ console.log(toBeChanged);\r\n            return resolve(toBeChanged);\r\n        } catch (err) {\r\n            return reject(err);\r\n            \/\/ console.error(err);\r\n            \/\/ return false;\r\n        }      \r\n    })\r\n}\r\n\r\nconst range = (start, end) =&gt; {\r\n    \/\/ console.log(start);\r\n    \/\/ console.log(end);\r\n    return Array(end - start + 1).fill().map((_, idx) =&gt; start + idx)\r\n}\r\n\r\nconst start = async() =&gt; {\r\n    try {\r\n        excludeWords.map( (word, index) =&gt; {\r\n            excludeWords[index] = excludeWords[index].toLowerCase();\r\n        });\r\n        \r\n        let modified_data_1 = await changeFormat(); \r\n        let maximum = 1;\r\n        modified_data = modified_data_1.map( (v, i) =&gt; {\r\n            if(v.split(' ').length &gt; maximum){\r\n                maximum = v.split(' ').length\r\n            }\r\n            if(excludeWords.indexOf(v.toLowerCase()) == -1) {\r\n                return v.toLowerCase();\r\n            }\r\n        });\r\n        text = text.split(' ');\r\n        if(maximum &gt; text.length){\r\n            maximum = text.length;\r\n        }\r\n        \/\/ console.log(maximum);\r\n        let temp = new Array(maximum);\r\n        temp.fill(2);\r\n        let answer = await temp.map( (v, i) =&gt; {\r\n            let tempArray = [...text, ...[]];\r\n            let tempArray1 = [...text, ...[]];\r\n            return checkSentence(tempArray, tempArray1, (maximum - i), modified_data);\r\n        })\r\n        return Promise.all(answer).then( (results) =&gt; {\r\n            let merged = [].concat.apply([], results);\r\n            \/\/ console.log('merged');\r\n            merged = new Set(merged);\r\n            merged = [...merged];\r\n            \/\/ console.log(merged);\r\n            merged.map((v, i) =&gt; {\r\n                if(v == undefined || v == null){\r\n                    return;\r\n                }\r\n                let temp1 = text[v].split('');\r\n                temp1[0] = temp1[0].toUpperCase();\r\n                text[v] = temp1.join('');\r\n            })\r\n            \/\/ console.log(text.join(' '));\r\n            return text.join(' ');\r\n        }).catch((err)=&gt;{\r\n            console.log(err);\r\n        })  \r\n    } catch (err) {\r\n        \/\/ console.error('here ERROR');\r\n        console.error(err);\r\n        return false;\r\n    }\r\n}\r\n\r\nlet excludeWords = ['Hard', 'Going'];\r\nlet text=\"united states to davis cup hard wazir Akbar Khan in bratislava\";\r\n\r\n( async () =&gt; {\r\n    let answer = await start();\r\n    console.log(answer);\r\n})();<\/code><\/pre>\n<\/div>\n<\/div><\/div>\n<div class=\"mt24\"><\/div>\n<\/div>\n<p>            <span class=\"d-none\" itemprop=\"commentCount\">9<\/span> <\/p><\/div>\n<\/div>\n<p>[ad_2]<\/p>\n<p>solved find cities and country name from string <\/p>\n","protected":false},"excerpt":{"rendered":"<p>[ad_1] It all starts from function start() at the bottom. For displaying purpose i&#8217;ve used a small dataset but you can require the data from the json file by using const data = require(&#8216;data.json&#8217;). I&#8217;ve tested for large dataset also, works like a charm. Hope it helps. const data = { &#8220;United States&#8221;:[ &#8220;Washington&#8221;,&#8221;Bratislava&#8221;,&#8221;Hard&#8221;,&#8221;Going&#8221;], &#8220;Afghanistan&#8221;: &#8230; <a title=\"[Solved] find cities and country name from string\" class=\"read-more\" href=\"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/\" aria-label=\"More on [Solved] find cities and country name from string\">Read more<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[320],"tags":[333,902,3073],"class_list":["post-19768","post","type-post","status-publish","format-standard","hentry","category-solved","tag-javascript","tag-node-js","tag-npm"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>[Solved] find cities and country name from string - JassWeb<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"[Solved] find cities and country name from string - JassWeb\" \/>\n<meta property=\"og:description\" content=\"[ad_1] It all starts from function start() at the bottom. For displaying purpose i&#8217;ve used a small dataset but you can require the data from the json file by using const data = require(&#039;data.json&#039;). I&#8217;ve tested for large dataset also, works like a charm. Hope it helps. const data = { &quot;United States&quot;:[ &quot;Washington&quot;,&quot;Bratislava&quot;,&quot;Hard&quot;,&quot;Going&quot;], &quot;Afghanistan&quot;: ... Read more\" \/>\n<meta property=\"og:url\" content=\"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/\" \/>\n<meta property=\"og:site_name\" content=\"JassWeb\" \/>\n<meta property=\"article:published_time\" content=\"2022-11-07T11:25:58+00:00\" \/>\n<meta name=\"author\" content=\"Kirat\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Kirat\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/\"},\"author\":{\"name\":\"Kirat\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\"},\"headline\":\"[Solved] find cities and country name from string\",\"datePublished\":\"2022-11-07T11:25:58+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/\"},\"wordCount\":60,\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"keywords\":[\"javascript\",\"node.js\",\"npm\"],\"articleSection\":[\"Solved\"],\"inLanguage\":\"en-US\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/\",\"name\":\"[Solved] find cities and country name from string - JassWeb\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\"},\"datePublished\":\"2022-11-07T11:25:58+00:00\",\"breadcrumb\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/solved-find-cities-and-country-name-from-string\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"[Solved] find cities and country name from string\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#website\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"name\":\"JassWeb\",\"description\":\"Build High-quality Websites\",\"publisher\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#organization\",\"name\":\"Jass Web\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\",\"url\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/wp-content\\\/uploads\\\/2021\\\/02\\\/jass-website-logo-1.png\",\"width\":693,\"height\":132,\"caption\":\"Jass Web\"},\"image\":{\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/logo\\\/image\\\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/#\\\/schema\\\/person\\\/65c9c7b7958150c0dc8371fa35dd7c31\",\"name\":\"Kirat\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1778218008\",\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1778218008\",\"contentUrl\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/wp-content\\\/litespeed\\\/avatar\\\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1778218008\",\"caption\":\"Kirat\"},\"sameAs\":[\"http:\\\/\\\/jassweb.com\"],\"url\":\"https:\\\/\\\/jassweb.com\\\/solved\\\/author\\\/jaspritsinghghumangmail-com\\\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"[Solved] find cities and country name from string - JassWeb","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/","og_locale":"en_US","og_type":"article","og_title":"[Solved] find cities and country name from string - JassWeb","og_description":"[ad_1] It all starts from function start() at the bottom. For displaying purpose i&#8217;ve used a small dataset but you can require the data from the json file by using const data = require('data.json'). I&#8217;ve tested for large dataset also, works like a charm. Hope it helps. const data = { \"United States\":[ \"Washington\",\"Bratislava\",\"Hard\",\"Going\"], \"Afghanistan\": ... Read more","og_url":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/","og_site_name":"JassWeb","article_published_time":"2022-11-07T11:25:58+00:00","author":"Kirat","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Kirat","Est. reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/#article","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/"},"author":{"name":"Kirat","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31"},"headline":"[Solved] find cities and country name from string","datePublished":"2022-11-07T11:25:58+00:00","mainEntityOfPage":{"@id":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/"},"wordCount":60,"publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"keywords":["javascript","node.js","npm"],"articleSection":["Solved"],"inLanguage":"en-US"},{"@type":"WebPage","@id":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/","url":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/","name":"[Solved] find cities and country name from string - JassWeb","isPartOf":{"@id":"https:\/\/jassweb.com\/solved\/#website"},"datePublished":"2022-11-07T11:25:58+00:00","breadcrumb":{"@id":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/jassweb.com\/solved\/solved-find-cities-and-country-name-from-string\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/jassweb.com\/solved\/"},{"@type":"ListItem","position":2,"name":"[Solved] find cities and country name from string"}]},{"@type":"WebSite","@id":"https:\/\/jassweb.com\/solved\/#website","url":"https:\/\/jassweb.com\/solved\/","name":"JassWeb","description":"Build High-quality Websites","publisher":{"@id":"https:\/\/jassweb.com\/solved\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/jassweb.com\/solved\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/jassweb.com\/solved\/#organization","name":"Jass Web","url":"https:\/\/jassweb.com\/solved\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/","url":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","contentUrl":"https:\/\/jassweb.com\/wp-content\/uploads\/2021\/02\/jass-website-logo-1.png","width":693,"height":132,"caption":"Jass Web"},"image":{"@id":"https:\/\/jassweb.com\/solved\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/jassweb.com\/solved\/#\/schema\/person\/65c9c7b7958150c0dc8371fa35dd7c31","name":"Kirat","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1778218008","url":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1778218008","contentUrl":"https:\/\/jassweb.com\/solved\/wp-content\/litespeed\/avatar\/1261af3c9451399fa1336d28b98ea3bb.jpg?ver=1778218008","caption":"Kirat"},"sameAs":["http:\/\/jassweb.com"],"url":"https:\/\/jassweb.com\/solved\/author\/jaspritsinghghumangmail-com\/"}]}},"_links":{"self":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19768","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/comments?post=19768"}],"version-history":[{"count":0,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/posts\/19768\/revisions"}],"wp:attachment":[{"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/media?parent=19768"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/categories?post=19768"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/jassweb.com\/solved\/wp-json\/wp\/v2\/tags?post=19768"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}