-1

The actual question is this (Stack Overflow limits characters in the questions):

How do i check if a string is present from a textbox in a (non-static) multidimensional array and then display the first item in the array from which the string was found in? (using jQuery)

For example:

(this is not what I'm actually doing, this is just for an example. I understand the example I'm providing has a much simpler solution. That's not what I'm after, though)

HTML

<input type="text" id="textbox">
<div id="output"></div>

JS:

var array = [
    ["that's an ice-cream topping","sprinkles","chocolate syrup"],
    ["that's a pizza topping","basil","cheese"],
    ["that's a car part","wheel","headlights","windshield wipers"]
];

('#textbox').keyup(function(){
    if(/*a match is found from the textbox in the array*/){
        ('#output').html(/*first item in the array the string was found in*/);
    } else {
        ('#output').html();
    }
});

This is what I'm trying to achieve: If a user types "sprinkles and stuff" in the textbox, as soon as "sprinkles" is typed, the output would display "that's an ice-cream topping."

Side-note: This assumes that typing "That's an ice-cream topping" into the textbox would also display "That's an ice-cream topping." This also assumes that the arrays could be changed and are never the same.

seagull
  • 11
  • 2
  • [`Array.prototype.find`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/find) – Andreas Nov 07 '15 at 09:34
  • This can be helpful to you: http://stackoverflow.com/questions/8809425/search-multi-dimensional-array-javascript – Ganesh Kumar Nov 07 '15 at 09:41

1 Answers1

0

You can do something like this:

$('#textbox').keyup(function () {
    var searchResult = findMatch($(this).val());
    if (searchResult) {
        $('#output').html(searchResult);
    } else {
        $('#output').html('');
    }
});

function findMatch(enteredString) {
    for (var i in array) {
        if ($.inArray(enteredString, array[i]) !== -1) {
            return array[i][0];
        }
    }
    return '';
}
Ganesh Kumar
  • 3,220
  • 1
  • 19
  • 27
  • Thank you so much! This works beautifully! However, I should clarify, I don't need to find an exact match. $.inArray looks for an exact match of the value. I just need to see if the value is present at the beginning of enteredString. Any help with this? – seagull Nov 07 '15 at 15:24
  • @Lolsdasfgnhg use `array[i].indexOf(enteredString) === 0` to match the beginning of the enteredString – Sean Nov 07 '15 at 15:35
  • @Sean do you mean in the if statement? If so, I'm afraid to say this doesn't work. – seagull Nov 07 '15 at 15:56
  • why doesn't work? have you tried? have you even used indexOf? `'hello world'.indexOf('hello') === 0` – Sean Nov 07 '15 at 15:58
  • [Yeah, I've tried.](http://jsfiddle.net/q06eqdsz/2/) And yes, I've used indexOf before. And if the value is there, it should return -1, right? By my understanding using === 0 would match it, but not return whether it's there or not. – seagull Nov 07 '15 at 16:03
  • @Lolsdasfgnhg, `indexOf` returns the 'index of' the matching value. If the value does NOT exist, it returns -1. – Rockster160 Nov 07 '15 at 16:11
  • @Rockster160 whoops, yes that's what I meant. I know the syntax is != -1, but that somehow registered in my mind as "returns -1" when I wrote that comment. – seagull Nov 07 '15 at 16:13
  • @Lolsdasfgnhg Yep! So by doing `=== 0`, you are checking that the substring is the very first string in the quote. Beware though, if there is a leading space, your indexOf will return `1`, not `0`. ' hello' vs 'hello'. Very easy to miss. – Rockster160 Nov 07 '15 at 16:15
  • @Rockster16 Then why in the example I linked prior,(http://jsfiddle.net/q06eqdsz/2/), It did not work? – seagull Nov 07 '15 at 16:19
  • @Lolsdasfgnhg Sorry for the late response. The reason is that you were getting the index of the array, but then checking `indexOf` for the /array/ not the /string/. See this updated fiddle: http://jsfiddle.net/q06eqdsz/3/ – Rockster160 Nov 10 '15 at 18:09