Create an array with objects in Javascript

3

I have defined the object, and array in scope.

//Define o array que deve ser preenchido com os objetos
var itensList = [];

//Define os argumentos do objeto
var oItem = {
    Codig: 0,
    Desc: "",
    Unid: "KG",
    Quant: 0,
    Custo: 0,
    IL: "",
    Centro: "",
    Saldo: o
}

Then, for each DHTMLX Grid row, it includes the results in the object ...

        //Para cada linha do grid, busca os resultados
        mygrid.forEachRow(function (id) {
            //Se o Codigo do item for maior que zero..
            if (mygrid.cells(id, 0).getValue() != 0) {
                // .. adiciona os valores ao objeto
                oItem.Codig = mygrid.cells(id, 0).getValue();
                oItem.Desc = mygrid.cells(id, 1).getValue();
                oItem.Unid = mygrid.cells(id, 2).getValue();
                oItem.Quant = mygrid.cells(id, 3).getValue();
                oItem.Custo = mygrid.cells(id, 4).getValue();
                oItem.IL = mygrid.cells(id, 5).getValue();
                oItem.Centro = mygrid.cells(id, 6).getValue();
                oItem.Saldo = mygrid.cells(id, 7).getValue();
                // Adiciona o objeto a lista
                itensList.push(oItem);
            }
        });

At the end of this program, if I have two rows filled in the Grid, it will have an array, with two objects, each with the parameters listed above, but the two get the same values, the values of the last line swept by the code, that is, two equal objects, when the lines are different. Debugging, I saw that by adding the object to the array with itensList.push(oItem); , it makes the object that already be there have the same values.

How to make each object have the values of each line?

    
asked by anonymous 09.04.2014 / 15:18

3 answers

3

This happens because you are inserting the same object into the vector. By changing the value of the field, it changes the references in the vector.

One way to resolve this would be to clone the object .

Another, simpler way would be to change the variable's declaration into if .

mygrid.forEachRow(function (id) {
  //Se o Codigo do item for maior que zero..
  if (mygrid.cells(id, 0).getValue() != 0) {
    var oItem = {};

    // .. adiciona os valores ao objeto
    oItem.Codig = mygrid.cells(id, 0).getValue();
    oItem.Desc = mygrid.cells(id, 1).getValue();
    oItem.Unid = mygrid.cells(id, 2).getValue();
    oItem.Quant = mygrid.cells(id, 3).getValue();
    oItem.Custo = mygrid.cells(id, 4).getValue();
    oItem.IL = mygrid.cells(id, 5).getValue();
    oItem.Centro = mygrid.cells(id, 6).getValue();
    oItem.Saldo = mygrid.cells(id, 7).getValue();

    // Adiciona o objeto a lista
    itensList.push(oItem);
  }
});
    
09.04.2014 / 15:39
2
The problem is that you are setting the same "instance" of the item object, and it will be modified in all your references, since it is being declared in a global scope in javascript.

In Javascript you do not need to declare the structure of your object, since the objects in javascript are dynamic, you can do just that:

//Para cada linha do grid, busca os resultados
mygrid.forEachRow(function(id) {
    //Se o Codigo do item for maior que zero..
    if (mygrid.cells(id, 0).getValue() != 0) {
        // .. adiciona os valores ao objeto
        var oItem = new Object(); // cria um objeto novo (uma nova instância)
        oItem.Codig = mygrid.cells(id, 0).getValue();
        oItem.Desc = mygrid.cells(id, 1).getValue();
        oItem.Unid = mygrid.cells(id, 2).getValue();
        oItem.Quant = mygrid.cells(id, 3).getValue();
        oItem.Custo = mygrid.cells(id, 4).getValue();
        oItem.IL = mygrid.cells(id, 5).getValue();
        oItem.Centro = mygrid.cells(id, 6).getValue();
        oItem.Saldo = mygrid.cells(id, 7).getValue();
        // Adiciona o objeto a lista
        itensList.push(oItem);
    }
});

And remove the declaration from the object:

//Define os argumentos do objeto
var oItem = {
    Codig: 0,
    Desc: "",
    Unid: "KG",
    Quant: 0,
    Custo: 0,
    IL: "",
    Centro: "",
    Saldo: o
}
    
09.04.2014 / 15:40
2

You need to clear the variable oItem , keep creating it out of looping and try this:

 //Para cada linha do grid, busca os resultados
        mygrid.forEachRow(function (id) {
            //Se o Codigo do item for maior que zero..
            if (mygrid.cells(id, 0).getValue() != 0) {
                // .. adiciona os valores ao objeto

                oItem = {}; // AQUI eu inicializo a variável

                oItem.Codig = mygrid.cells(id, 0).getValue();
                oItem.Desc = mygrid.cells(id, 1).getValue();
                oItem.Unid = mygrid.cells(id, 2).getValue();
                oItem.Quant = mygrid.cells(id, 3).getValue();
                oItem.Custo = mygrid.cells(id, 4).getValue();
                oItem.IL = mygrid.cells(id, 5).getValue();
                oItem.Centro = mygrid.cells(id, 6).getValue();
                oItem.Saldo = mygrid.cells(id, 7).getValue();
                // Adiciona o objeto a lista
                itensList.push(oItem);
            }
        });

Or not to lose the properties set initially:

 //Para cada linha do grid, busca os resultados
        mygrid.forEachRow(function (id) {
            //Se o Codigo do item for maior que zero..
            if (mygrid.cells(id, 0).getValue() != 0) {
                // .. adiciona os valores ao objeto

                var oItem = { // AQUI eu inicializo a variável
                  Codig: 0,
                  Desc: "",
                  Unid: "KG",
                  Quant: 0,
                  Custo: 0,
                  IL: "",
                  Centro: "",
                  Saldo: o
                } 

                oItem.Codig = mygrid.cells(id, 0).getValue();
                oItem.Desc = mygrid.cells(id, 1).getValue();
                oItem.Unid = mygrid.cells(id, 2).getValue();
                oItem.Quant = mygrid.cells(id, 3).getValue();
                oItem.Custo = mygrid.cells(id, 4).getValue();
                oItem.IL = mygrid.cells(id, 5).getValue();
                oItem.Centro = mygrid.cells(id, 6).getValue();
                oItem.Saldo = mygrid.cells(id, 7).getValue();
                // Adiciona o objeto a lista
                itensList.push(oItem);
            }
        });
    
09.04.2014 / 15:42