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

The best way to check the best match contains String?

I have a String and want to determine whether the best String is contained by another String.

I’ll try to explain by JS code like this:

function A(){
    var str = "The best way to check the best match contains String?";

    if(str.includes("The")){ //<== This is not the best match, but currently return this
        return "1";
    }
    if(str.includes("The best")){
        return "2";
    }
    if(str.includes("The best way1")){
        return "3";
    }
    if(str.includes("The best way")){ //<=== Should to return this
        return "4";
    }
}

I don’t want to swap the if conditions by hand. Because maybe the list check and results want to return is dynamic or load from somewhere like:

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 objCheck = {
        "The": "1",
        "The best": "2",
        "The best way1": "3",
        "The best way": "4"
    }

Any idea for this?

>Solution :

You can use Object.entries() on your object to get an array of key-value pairs of the shape:

[["The", "1"], ["The best": "2"], ...]

Above, an inner array represents an entry (ie: key, value from your object). Once you have this array, you can .sort() based on the length, so that the entries with the longest key appear first in your array. Once sorted, you can use .find() to find the first entry that has a key that is included in your search str. Lastly, you can use ?.pop() to grab the number value associated with the key that is found. The ?. here is the optional chaining operator which is used to ensure that we don’t try and call .pop() if .find() is unable to find any entries:

const objCheck = {
  "The": "1",
  "The best": "2",
  "The best way1": "3",
  "The best way": "4"
};

function findBestMatch(str, obj){
  return Object.entries(obj)
    .sort(([a], [b]) => b.length - a.length)
    .find(([substr]) => str.includes(substr))
    ?.pop();
}

var str = "The best way to check the best match";
console.log(findBestMatch(str, objCheck));

If you need something a little more efficient, you can avoid the sorting by maintaining the max value seen so far as you loop through your array:

const objCheck = {
  "The": "1",
  "The best": "2",
  "The best way1": "3",
  "The best way": "4"
};

function findBestMatch(str, obj){
  let res, best = -Infinity, entries = Object.entries(obj);
  for(const [substr, num] of entries) {
    if(substr.length > best && str.includes(substr)) {
      best = substr.length;
      res = num;
    }
  }
  
  return res;
}

var str = "The best way to check the best match";
console.log(findBestMatch(str, objCheck));
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