0

I have an array that contains some values. I want to be able to check if the value in the input field exists within the array. My code isn't returning anything now.

Html

<input type="text" id="str_search" value=" "/>
<input type="button" id="search_str" value="search" />

Javascript

var array = ["php", "jquery", "java"];
var src_keyword = "";
$(document).ready(function () {
    $("#search_str").click(function () {
        src_keyword = $("#str_search").val();
        // alert(find(array,src_keyword));
        find(array, src_keyword)
    });
});

function find(arr, src_keyword1) {
    var result = [];
    alert(src_keyword1);
    // src_keyword1="oo";
    for (var i in arr) {
        var search = new RegExp(src_keyword1, "gi");
        if (arr[i].match(search)) {
            alert(arr[i])
            result.push(arr[i]);
        }
    }
    return result;
}

thanks in advance

pmandell
  • 4,288
  • 17
  • 21
Seenu
  • 83
  • 1
  • 10

5 Answers5

2

Fork:- http://jsfiddle.net/jfy8J/

Probably why your code is not working is because you have a space in the textbox value and you are not trimming it.

var array = ["php", "jquery", "java"];
var src_keyword = "";
$(document).ready(function () {
    $("#search_str").click(function () {
        src_keyword = $("#str_search").val().trim();
        var res = $.grep(array,function(obj,_){
            return obj === src_keyword;
        });
       if(res.length == 0) alert('not found'); else alert('found');
    });
});

Update getting not found values :- http://jsfiddle.net/ELqZS/

var array = ["php", "jquery", "java"];
var src_keyword = "";
$(document).ready(function () {
    $("#search_str").click(function () {
        src_keyword = $("#str_search").val().trim();
       var items = $.grep(array,function(obj,_){
            return obj !== src_keyword;
        });
    alert(items);
    });
});

Update 2 returns the items from the array that has the search text http://jsfiddle.net/8FJYS/

var array = ["php", "jquery", "java"];
var src_keyword = "";
$(document).ready(function () {
    $("#search_str").click(function () {
        src_keyword = $("#str_search").val().trim();
      var items =  array.filter(function(o, _){
            return o.indexOf(src_keyword) > -1;
        });
    alert(items);
    });
});
PSL
  • 123,204
  • 21
  • 253
  • 243
1

Change your for loop from

for (var i in arr)

to

for (var i=0; i < arr.length; i++)

The former loop iterates over all the properties of the object as well as the value of each indexed element. For example

var a = [7,8,9,0];
a.blah = "aoeusnth";
for (var prop in a) {
    console.log(prop);
}

Will log

7
8
9
0
"blah"

to the console.

You are using the variable "i" to access the values of the array, but in your code "i" is the value already. What you want is to use the index of the item in order to access the item.

schellsan
  • 2,164
  • 1
  • 22
  • 32
0

Just use .indexOf()

$("#search_str").click(function () {
  src_keyword = $("#str_search").val().trim();
  alert(array.indexOf(src_keyword) !== -1);
});
thinklinux
  • 1,287
  • 9
  • 13
0

Reverse the testing process. Right now, your testing the result strings for the search strings. You want to reverse that.

Use the regexp test() method. So...

var search = new RegExp(src_keyword1, "gi");
if(search.test(arr[i])){....}

Of course, you can now take the RegExp constructor out of the loop, among other small changes....

anson
  • 4,156
  • 2
  • 22
  • 30
0

I think you want to use Array.indexOf rather than a regex solution.

Summary

Returns the first index at which a given element can be found in the array, or -1 if it is not present.

<input type="text" id="str_search" value=" " />
<input type="button" id="search_str" value="search" />

var array = ["php", "jquery", "java"];
var src_keyword = "";
$(document).ready(function () {
    $("#search_str").click(function () {
        src_keyword = $("#str_search").val().trim();
        alert(array.indexOf(src_keyword) !== -1);
    });
});

on jsfiddle

Xotic750
  • 22,914
  • 8
  • 57
  • 79