Home > Software engineering >  How handle $.getJSON file with strings and integer?
How handle $.getJSON file with strings and integer?

Time:10-23

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 >';

            $.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

CodePudding user response:

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

  • Related