0

I am trying to look for texbox values matching an array I have provided a code snippet below:

$('#message').on('keyup', function () {
    suggest_array_sample = [
        { array_val: "#{person1}" },
        { array_val: "#{person2}" },
        { array_val: "#{person3}" }
    ];

    found_variable_array = [];
    $.each(suggest_array_sample, function (key, value) {
        console.log(value);
        if ($.inArray(value, textbox_value)) {
            console.log('found');
            found_variable_array.push(value);
        } else {
            console.log('not found');
        }
    })

    console.log(found_variable_array);
});
<textarea id="message"></textarea>

The problem is it always return the whole array instead of just the matches the ouput should be when I type #{person1} on the textbox the output should be

[{array_val:"#{person1}"}] //expected output

[{array_val:"#{person1}"},{array_val:"#person2"}]// expected output when two or more matches are found on the textbox

instead of

[{array_val:"#{person1}"},]{array_val:"#{person2}",{array_val:"#{person3}"}] //current output

is this possible using the inArray() or do I need to change the code.

Community
  • 1
  • 1
Alfred M.
  • 159
  • 2
  • 14
  • Possible duplicate of [jQuery.inArray(), how to use it right?](https://stackoverflow.com/questions/18867599/jquery-inarray-how-to-use-it-right) – Dan O May 29 '18 at 16:40
  • Also can't compare object to input value which is a string which is what you are trying to do with $.inArray – charlietfl May 29 '18 at 16:41

4 Answers4

0

use filter method of Array.

yourArray.filter ( yourArrayModel => yourArrayModel.fieldValue === yourSearchValue )

In your case yourSearchValue can be “{#person1}”

For more information, look for filter method documentation, i hope this is what you want.

Abhishek Kumar
  • 2,501
  • 10
  • 25
0

It's not entirely clear what you're trying to achieve. I've written something using $.inArray that tells you the array index of the found value. You need to use .map() on the array to extract the val you want.

EDIT:

From what I understood of your comment, I've now had the value be added to found_value_array each time the value is found.

Or is it that you want an array to be returned because the same value might appear multiple times?

let found_variable_array = [];

$('#message').on('keyup',function(){
  suggest_array_sample = [
       {array_val:"#{person1}"},
       {array_val:"#{person2}"},
       {array_val:"#{person3}"}
      ]
        
        let index = $.inArray($(this).val(), suggest_array_sample.map(o => o.array_val));
        
        if (index >= 0) found_variable_array.push(suggest_array_sample[index]);
        
        console.log(found_variable_array);
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<textarea id="message"></textarea>
Michael Beeson
  • 2,840
  • 2
  • 17
  • 25
  • for example I typed #{person1} on the texbox given the condition it should return a match together with the value so I can store it to the new array – Alfred M. May 29 '18 at 16:52
  • You want the array to persist outside of that function? Or is it a new array every time? – Michael Beeson May 29 '18 at 16:57
  • it should just override the current value set on the found_variable_array meaning everytime it matches the suggest_array when you type on the textbox it should get stored – Alfred M. May 29 '18 at 17:01
  • I've made an edit, but that last comment has confused me again. If you're only ever storing one value, why have it as an array? – Michael Beeson May 29 '18 at 17:02
  • its not one value haha im sorry for the confusion ill try to explain more clearly say you type #{person1} and #{person2} on the texbox since its a match it should get stored the reason im using array is because it can be multiple values depending on the value of the suggest_array – Alfred M. May 29 '18 at 17:05
  • I'm afraid I'm more confused than ever. Gotta leave the house now, but if nobody answers this later I'll have another look! – Michael Beeson May 29 '18 at 17:07
0

$.inArray return a position, if not found return -1 else return >= 0

The $.inArray() method is similar to JavaScript's native .indexOf() method in that it returns -1 when it doesn't find a match. If the first element within the array matches value, $.inArray() returns 0

Try this code

$('#message').on('keyup', function () {
    textbox_value = $(this).val();
    suggest_array_sample = ["#{person1}", "#{person2}", "#{person3}"];

    console.log($.inArray(textbox_value, suggest_array_sample));
});
Hugo
  • 9
  • 4
0

After combining ideas presented here this method work for me

match_value = suggest_array.filter(function(result){
       
       if(textbox_value){
            return textbox_value.search(result)>-1
          }

          else{
            return false
          }
      }) 
      
console.log(match_value);
Alfred M.
  • 159
  • 2
  • 14