Error in array travel


I have a long and complex script (for me to say), in which I have an algorithm on a JSON array obtained with php, which is a list of products.

The only thing that gives me an error is in showbizublista, this function takes an array, a from and an up and shows the html table with said content.

The algorithm or what I wanted to do was filter by category and subcategory and search by name. I know it complicates me a little but otherwise I could leave the search by incremental text about the 5000 records, and the combos only to filter and travel with the mouse. But I wanted to ask them for help to see if I solve.

the code that gives an error is the following:

function mostrarsublista(data,desde,hasta){

        $("#resultado tbody").empty();
    for (var i = desde; i < hasta; i++) {
                var newRow =
                    "<tr>" +
                    "<td>" + data[i].idproducto + "</td>" +
                    "<td>" + data[i].nombre + "</td>" +
                    "<td>" + data[i].marca + "</td>" +
                    "<td>" + data[i].categoria + "</td>" +
                    "<td>" + data[i].subcategoria + "</td>" +
                    "<td>" + data[i].precio + "</td>" +
                    "<td>" + data[i].aliiva + "</td>" +
                    "<td><input type='radio' id='"+data[i].idproducto+"' name='seleccion'/></td>"+
                $(newRow).appendTo("#resultado tbody");                 

And the error I get is: TypeError: data [i] is undefined On the other hand..I made a consoe.log (data) and the array arrives perfect.

asked by Caruso 02.11.2018 в 13:10

1 answer


The problem is that the cycle is run more times than data exist. To solve it, check with a conditional the length of data so that it only enters the condition if it is less than or equal to hasta .

answered by 02.11.2018 / 14:17