Unable to read property

1

I have a problem that seems simple but I can not find the solution.

table = "<div><table class='table table-hover'>";
dados = "";

for(var i = 0; i <= retorno.dados.length; i++)
{
    dados = dados + "<tr><td class='col-md-11'>" + retorno.dados[i]['user_id'] + "    </td>";         
    // console.log(retorno.dados[i]['user_id']);
}

The error in the console is as follows: Uncaught TypeError: Can not read property 'user_id' of undefined

    
asked by anonymous 06.10.2015 / 17:43

1 answer

0

When you walk through an array with a for(var i=0;i<= loop, you must stop before i is equal to the last element in the array.

So say in MDN on .length :

  The length property [...] is always numerically greater than the highest index in the array.

That is, the length property is always greater than the index of the last element of the array, so you can not use <= .

    
06.10.2015 / 18:06