0

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;
        }
  • Return `arrayToSearchIn[i]` instead of `true` ? Where is `searchLocation` defined ? – guest271314 Oct 17 '15 at 16:01
  • Sorry I didint include the function for finding the location, – JohnTheKnow Oct 17 '15 at 16:04
  • Is requirement to return entire string matched , or only remainder of string matched within array ? Can include example of string to match , `appointmentArr` array at Question ? – guest271314 Oct 17 '15 at 16:05
  • No just a word, so for example if they type in a "america" in the search field and then click search the results should bring back the entire string of where "america" is found, so the result would bring up any strings with that matching word...etc First Name: John, Location:"america: – JohnTheKnow Oct 17 '15 at 16:12
  • So a typical string would be like var dummyAppointments = new Array('25/05/2015','0800','0900','TSV5555','America') – JohnTheKnow Oct 17 '15 at 16:14
  • _"So a typical string would be like var dummyAppointments = new Array('25/05/2015','0800','0900','TSV5555','America') "_ ? `dummyAppointments` appear to be array ? – guest271314 Oct 17 '15 at 16:16

4 Answers4

0

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);
JsingH
  • 189
  • 7
  • Hmm, doesn't seem to be working, I've just tried the method of if (appointmentArr.indexOf(searchingforinlowerCase) >= 0) { alert("Found") /* found substring Wor */ } – JohnTheKnow Oct 17 '15 at 15:56
0

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.

JsingH
  • 189
  • 7
  • Ok this finds it, but how do i get the string that its related to be printed out ha? – JohnTheKnow Oct 17 '15 at 16:17
  • Great thanks! now if i wanted to be able to print out multiple matches, would i need to push each match to a new array in the function? – JohnTheKnow Oct 17 '15 at 17:31
  • Awesome! if I were to write these strings to a table, do you have to do it within the function or can you do it through the variable? – JohnTheKnow Oct 17 '15 at 17:57
  • You always separate the computation and display code. It is better to return an array of matched strings from search function and then display all returned in a table. – JsingH Oct 17 '15 at 18:02
  • How do you return the index and the strings? is it arrayToSearchIn[i] to be returned or just i? or like return arrayToSearchIn[i],i; – JohnTheKnow Oct 17 '15 at 18:33
  • I rejig'd code to meet your requirements. https://jsfiddle.net/hd5jxn7s/ You cannot return 2 things in a single return statement unless you contain them in some form. – JsingH Oct 17 '15 at 18:55
  • Thankyou for your help, much appreciated . I Just need to add each string to a table now =) – JohnTheKnow Oct 17 '15 at 19:10
0

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])
guest271314
  • 1
  • 15
  • 104
  • 177
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.

show-me-the-code
  • 1,553
  • 1
  • 12
  • 14