Insert data into an array inside a json

1

I need to store data in an array that is inside a json, that is, I will have several users that will have data

let nombresUsuarios={
                datos:[{nombre :''},
                       {acierto:''},
                       {fallo  :''}]};

how to fill it?

my intent is:

for (var i = 0; i < rows.length; i++) {
                        nombresUsuarios.datos.nombre=rows[i].nombre;
                        nombresUsuarios.datos.acierto=rows[i].dato1;
                        nombresUsuarios.datos.fallo=rows[i].dato2;
                    };  

but it does not fill anything, how do I do it?

    
asked by hubman 08.01.2017 в 07:18
source

1 answer

4

To insert the data into a json, use the push .

It should work as follows, try and tell us how it went.

var nombresUsuarios={datos:[{nombre :''},{acierto:''},{fallo:''}]};

var obj = JSON.parse(nombresUsuarios);

for (var i = 0; i < rows.length; i++) {
     obj['datos'].push({"nombre":rows[i].nombre,"acierto":rows[i].dato1,"fallo":rows[i].dato2});
};

nombresUsuarios= JSON.stringify(obj);
    
answered by 08.01.2017 / 07:45
source