简体   繁体   中英

Javascript Search for a match and output the whole string that the matching word is apart of

how would you go about outputting the found output, including the rest of the string its apart of? The array is just full of strings. Thanks

    var searchingfor = document.getElementById('searchfield').value;
        var searchingforinlowerCase = searchingfor.toLowerCase();
        var searchDiv = document.getElementById('searchDiv');
        var convertarraytoString = appointmentArr.toString();
        var arraytolowerCase = convertarraytoString.toLowerCase();
        var splitarrayString = arraytolowerCase.split(',')



        if(search(searchingforinlowerCase, splitarrayString) == true) {
                alert( searchingforinlowerCase + ' was found at index' + searchLocation(searchingforinlowerCase,splitarrayString) + ' Amount of times found = ' +searchCount(searchingforinlowerCase,splitarrayString));


        function search(target, arrayToSearchIn) {

        var i;

          for (i=0; i<arrayToSearchIn.length; i++)
        {   if (arrayToSearchIn[i] == target && target !=="")
        return true;
        }

You can do like this

var test = 'Hello World';
if (test.indexOf('Wor') >= 0)
{
  /* found substring Wor */
}

In your posted code you are converting Array to string and then again converting it back to Array using split(). That is unnecessary. search can be invoked as

search(searchingforinlowerCase, appointmentArr);

Try this

if(search(searchingforinlowerCase, appointmentArr) == true) {
                    alert( searchingforinlowerCase + ' was found at index' + searchLocation(searchingforinlowerCase,splitarrayString) + ' Amount of times found = ' +searchCount(searchingforinlowerCase,splitarrayString));


function search(target, arrayToSearchIn) {
var i; 
for (i=0; i<arrayToSearchIn.length; i++)
{   if (arrayToSearchIn[i].indexOf(target >= 0))
        return true;
}
    return false;
}

This code will help you find that a match is present. You can update code to display full text where match was found. Original posted code was comparing entire string rather than partial match.

Try utilizing Array.prototype.filter() , String.prototype.indexOf()

 // input string var str = "america"; // array of strings var arr = ["First Name: John, Location:'america'", "First Name: Jane, Location:'antarctica'"]; // filter array of strings var res = arr.filter(function(value) { // if `str` found in `value` , return string from `arr` return value.toLowerCase().indexOf(str.toLowerCase()) !== -1 }); // do stuff with returned single , or strings from `arr` console.log(res, res[0]) 

The following will look for a word in an array of strings and return all the strings that match the word. Is this something you are looking for?

var a  = ["one word", "two sentence", "three paragraph", "four page", "five chapter", "six section", "seven book", "one, two word", "two,two sentence", "three, two paragraph", "four, two page", "five, two chapter",];

function search(needle, haystack){
    var results = [];
  haystack.forEach(function(str){
        if(str.indexOf(needle) > -1){
            results.push(str);
        }
  });
    return results.length ? results : '';
};

var b = search("word", a);
console.log(b);

Here's the fiddle to try.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM