88willr
88willr

Reputation: 148

Match array with strings from another array by word relevance

var myArray = [
    ["good morning", "am", "morning"],
    ["good night", "night", "evening"],
    ["good day", "day is good", "it's a good day"]
];

function compare(entry, string) {
    for (var x in entry) {
        strings = string.split(' ');
        array1 = entry[x][0].split(' ');
        if (strings.every(s => array1.indexOf(s) !== -1)) {
            items = entry[x].slice(1);
            return items
        } else {
            //return 'not found'
        }
    }
}
$("#input").keydown(function(e) {
    keyword = $(this).val();
    if (e.which === 13) {
        $(this).val("");
        text = keyword.toLowerCase();
        result = '<div>' + compare(myArray, text) + '</div>'
        $('#results').append(result);
    }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="input" type="text" placeholder="Search..." autocomplete="off" />
<div id="results" class="reset"></div>

strings = Keywords from input box is converted into an array

array1 = Each first items from group of arrays are converted into an array

...compare them then return items from the same group where it found the matched keywords. I used .slice(1) so it wont return the first item of each group.

Using every() is the closest iv got after trying includes(), .some() etc...

I have two problems on the above code:

  1. it will return undefined if the keyword is more than the words present in the array.

it should return true if keywords are:

good,

good morning dude,

good night folks,

good day people

  1. if I put "else" on the condition, it wont function same as without "else"...

thanks for your help..

Upvotes: 3

Views: 841

Answers (2)

Randy Casburn
Randy Casburn

Reputation: 14165

Use .map() twice, see if the string is included in the inner array value, then flatten the array and filter out non-string values.

This code finds matching words in each string. The ternary either returns the string (if found) or returns null if not found. That gives an array of arrays. So they get flattened into a single array. Finally, the null values are filtered out.

here is one way to accomplish your goal:

var myArray = [
  ["good morning", "am", "morning"],
  ["good night", "night", "evening"],
  ["good day", "day is good", "it's a good day"]
];

function compare(entry, string) {
  let out = string
             .split(' ')
             .map(str => 
                   entry.map(e => 
                          e.map(f => (f.includes(str)) ? f : null))
              .flat()
              .filter(a => a !== null));
  return out.join(', ');
}

compare(myArray, 'good night dude');

$("#input").keydown(function(e) {
  keyword = $(this).val();
  if (e.which === 13) {
    $(this).val("");
    text = keyword.toLowerCase();
    result = '<div>' + compare(myArray, text) + '</div>'
    $('#results').append(result);
  }
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input id="input" type="text" placeholder="Search..." autocomplete="off" />
<div id="results" class="reset"></div>

Upvotes: 1

cabrillosa
cabrillosa

Reputation: 155

Return some value that represents "not found" and check in keydown() function before appending the text to div

var myArray = [
    ["good morning", "am", "morning"],
    ["good night", "night", "evening"],
    ["good day", "day is good", "it's a good day"]
];

function compare(entry, string) {
    for (var x in entry) {
        strings = string.split(' ');
        array1 = entry[x][0].split(' ');
        if (strings.every(s => array1.indexOf(s) !== -1)) {
            items = entry[x].slice(1);
            return items
        } else {
            return -1;
        }
    }
}
$("#input").keydown(function(e) {
    keyword = $(this).val();
    if (e.which === 13) {
        $(this).val("");
        text = keyword.toLowerCase();
        var matched = compare(myArray, text);
        if(matched!=-1){
          result = '<div>' + matched + '</div>'
        } else {
          result = '<div>Not Found</div>'
        }
       
        $('#results').append(result);
    }
});

Upvotes: 0

Related Questions