How can I push a javascript fix of type {}?

0

I'm trying to generate a dynamic array of type {}

tabla.each(function() {

    var ID = $(this).find('td:eq(0)').html();
    var fechaingre = $(this).find("input[id*='dfi']").val();
    var fechafin = $(this).find("input[id*='dff']").val();
    var canadul = $(this).find("select[id*='adul']").val();
    var canchil = $(this).find("select[id*='chil']").val();

    var pre = sessionStorage.key(au);
    var valorprecio = sessionStorage.getItem(pre);

    item = {};
    if (ID !== '') {
        item["id"] = ID;
        item["fechain"] = fechaingre;
        item["fechafin"] = fechafin;
        item["pre"] = valorprecio;
        item["adult"] = canadul;
        item["chil"] = canchil;
        var room = sessionStorage.getItem("users");
        array_room = JSON.parse(room);
        var habitacion = array_room[au];
        item["idroom"] = habitacion.id;
        item.push(item);
    }
    au++;
});
console.log(item);

However when printing in console item shows error.

what would be the correct way to generate a dynamic array of the type

{
    "ID": "1",
    "fechain": "20160101",

},
{
  "ID": "2",
    "fechain": "20160201"
}
    
asked by lucho 20.01.2017 в 12:12
source

4 answers

0

As Enmanuel told you, it is not possible to use push (), nor its pop counterpart () as methods of arrangement

What if you can get to do if you need, to say a dynamic property management, is to treat the object, using fixation notation against it, applying this treatment to its properties

var objetoDinamico = {}; //hasta aqui es objeto
for(var i = 0; i<10;i++)
{
  objetoDinamico['propiedad_'+i] = 'valor_'+i;
  console.log('Propiedad agregada '+i +' - valor:  '+objetoDinamico['propiedad_'+i])
}
    
answered by 20.01.2017 / 16:22
source
2

You can not do push on a {} because it is an object. To do so you must use an array or array (a collection of objects), in which case you must use a [] :

So, on the one hand you create the objects and on the other you insert them in the arrangement:

Example:

var obj = {};
var arreglo = [];

obj['nombre'] = 'Jose';
obj['apellido'] = 'Perez';

// aqui puedes hacer push
arreglo.push(obj);

var obj2 = {};

obj2['nombre'] = 'Juan';
obj2['apellido'] = 'Gomez';

// agregas un segundo elemento al arreglo
arreglo.push(obj2);

console.log(arreglo);

Keep in mind that the correct format of a JSON collection is [ {}, {} ] , that is, it is surrounded by [ and ] , something you did not put in the example of the question. I hope it serves you, salu2

    
answered by 20.01.2017 в 12:32
1

This one that tells you that the function push is only available for fixes, in your case to solve your problem is to create another variable of type array that contains each item.

var items = [];
tabla.each(function() {
var ID = $(this).find('td:eq(0)').html();
var fechaingre = $(this).find("input[id*='dfi']").val();
var fechafin = $(this).find("input[id*='dff']").val();
var canadul = $(this).find("select[id*='adul']").val();
var canchil = $(this).find("select[id*='chil']").val();

var pre = sessionStorage.key(au);
var valorprecio = sessionStorage.getItem(pre);

var item = {};
if (ID !== '') {
    var room = sessionStorage.getItem("users");
    var array_room = JSON.parse(room);
    var habitacion = array_room[au];

    item["id"] = ID;
    item["fechain"] = fechaingre;
    item["fechafin"] = fechafin;
    item["pre"] = valorprecio;
    item["adult"] = canadul;
    item["chil"] = canchil;
    item["idroom"] = habitacion.id; // Posible error 'undefined'
    items.push(item);
}
au++;
});
console.log(items);

Another recommendation would be to validate that there is at least one record in "array_room".

    
answered by 20.01.2017 в 17:33
0

to make a push to a javascript fix is like this:

arreglo.push({ var1, var1, var3 });
    
answered by 20.01.2017 в 16:31