Search code examples
javascriptjqueryjsongetjson

How handle $.getJSON file with strings and integer?


how can I parse Integer from the JSON file? Or what is the problem below?

$('#search').keyup(function(){

        var searchField = $('#search').val();

        var myExp = new RegExp(searchField, "i");

     $.getJSON('data.json', function(data){
            if(searchField === '')  {
                $('#update').html('');
                return;}
            var output ='<ul class="searchresults">';

            $.each(data, function(key, val){

            if(val.number.search(myExp) != -1) {
                output += '<li>';
                output += '<p>' + val.number + '</p>';
                output += '<p>' + val.name + '</p>';
                output += '</li>';
            }
            });

            output += '</ul>';
            $('#update').html(output);

        });

    });

What is missing?

Error is:

Uncaught TypeError: val.number.search is not a function

Many thanks in advance!

Br


Solution

  • Probably it's a number, numbers don't have a search method. You could use .toString().search(...) to convert it to a string first.