error when traversing array

0

Good I have a problem when walking my arrya, it marks me indefinitely, I have always done it like this and I have never had any problems, it is in MVC

ajax

 function cargar_requerido(accion)
{
    var accion = accion;
    $.ajax({
        type: 'POST',
        datatype: "json",
        url: base_url +'ctrl_prueba/cargar_prueba',
        success: function(data){
            console.log("print data");
           console.log(data); // Si lo imprime bien
           if(accion== 'cargar')
           {
           cargar_requerido_prueba(data);
           }
        }
    });
}

function cargar_requeridoprueba(data)
{
    var html = '';
console.log(data);//si lo imprime bien
    console.log(data.length);// imprime 245 registros y deberia de ser 1 registro

    $('#body_table').empty();
    for(var i=0; i<data.length; i++)
    {
        console.log(data[i].fecha_registro);// imprime indefinido

  html+= '<tr><td>'+data[i].fecha_registro+
        '</td><td>'+data[i].dias+
        '</td><td>'+data[i].contacto_nombre+
        '</td><td>'+data[i].modelo+
        '</td></tr>';
    }
    $('#body_table').html(html);
}


[{
    "fecha_registro": "2018-10-08",
    "dias": "1",
    "contacto_nombre": "dfdgfdgdfg",
    "modelo": "\tBr-v Prime Blanco",
}]

when I print the data.length says 245

Controller

function cargar_prueba()
    {

        $response = $this->valuaciones->cargar_prueba();
        //print_r($response);
        echo json_encode($response);
    }

So I printed a single word

    
asked by Juan Jose 09.10.2018 в 17:30
source

2 answers

1

Most likely, that particular object in the i position does not have that property.

if(data[i].fecha_registro == undefined)
{
   console.log(data[i]);
}

With this you will know if the other properties of this object have value but the property date_register does not.

    
answered by 09.10.2018 / 17:43
source
0

What happens is that you have a json in the form of an array and everything is a single element, that's why the data.length shows you the whole element, to show it you must iterate it:

  for(var key in data)
 {
   console.log(key+" - "+data[key]);
   }
    
answered by 09.10.2018 в 18:06