S
S
suhuxa12018-03-14 21:30:21
JavaScript
suhuxa1, 2018-03-14 21:30:21

How to implement search through jquery by integer?

Hey!
There was a big problem. I've always done a search like this:

var search = $.trim($('input').val()),
        reg = new RegExp(search, 'i');

$.each(search_data, function(key,val) {
        if (val.name.search(reg) != -1 || val.code.search(reg) != -1) {
            data[key] = {
                "name"  :  val.name,
                "code"  :  val.code
            };
        }
    });


that is, everything is simple - we take the value from the input, put it into a regular expression, and then look for occurrences in the loop. Everything has always worked fine for the lines, but here is the val.code field (here the code is presented as an integer). And an error pops up:
Uncaught TypeError: val.code.search is not a function


How to overcome it? Methods index, exec, test and so on give the same error.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
T
twobomb, 2018-03-14
@suhuxa1

Maybe

val.code.toString();
val.code.toString().search...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question